【问题标题】:Request to web services restful with pojo object and method post使用 pojo 对象和方法发布对 web 服务的请求
【发布时间】:2016-04-16 22:57:00
【问题描述】:

我创建了一个安静的网络服务:

@POST
@Path("grd")
@Consumes("application/json")
@Produces("text/plain")
public String guardarDato(PostParams jsonObject) {
/*
 something here
}

PostParams 是一个 pojo:

public class PostParams {

    private final  Map<String, String> postParamsList;

    public PostParams() {
        postParamsList = new HashMap<>();
    }

    public void addParameter(String name, String value) {
        postParamsList.put(name, value);
    }

    public String getPostParamsList(String name) {
        return postParamsList.get(name);
    }

    public void getPostParamsMap() {
        if (postParamsList.isEmpty()) {
            System.out.println("Parametros vacios");
        } else {
            for (String key : postParamsList.keySet()) {
                System.out.println("Clave: " + key + " -> Valor: " +         postParamsList.get(key));
            }
        }
    }
}

我正在尝试使用 HttpUrlConnection 从 android 调用此 Web 服务, 但我的对象在我的网络服务中是 null pojo。

 try {

        URL url = new URL("http://localhost:8080/VfqCh/hgt/opli/grd");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
       // conn.setRequestProperty("Accept", "application/json");

        PostParams postParams = new PostParams();
        postParams.addParameter("h", "51rt3");
        postParams.addParameter("x", "dsfsd8698sdfs");
        postParams.addParameter("ax", "Dairon");
        postParams.addParameter("tf", "D");
        // String input = "{\"qty\":100,\"name\":\"iPad 4\"}";
        Gson gson = new Gson();
        String gString = gson.toJson(postParams, PostParams.class);
        try (OutputStreamWriter printout = new OutputStreamWriter(conn.getOutputStream())) {
            printout.write(gString);
            printout.flush();
        }

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }

}

信息被发送到 Web 服务,但对象为空。 如果我更改在 Web 服务中接收的对象类型(如果它有效)!:

@POST
@Path("grd")
@Consumes("application/json")
@Produces("text/plain")
public String guardarDato(String jsonObject) {
/*
 something here
}

但我需要在网络服务中接收一个对象!有可能吗?

【问题讨论】:

  • 你为一个简单的Hashmap做了一个POJO?
  • 是的,是 hashmap 的 pojo。
  • 球衣?雄猫? @POST@Path("grd") 的定义是什么?
  • javax.ws.rs.POST 和 javax.ws.rs.Path;

标签: java web-services rest httpurlconnection javax.ws.rs


【解决方案1】:

您为 HashMap 创建了一个 POJO,但您可以只使用 Gson 已经提供的 JsonObject,它基本上就像一个 HashMap。然后,你 toString 它,你有一个可以发送到服务器的 JSON 字符串。不需要Gson.toJson 并担心它是否被正确转换。

您的服务器正在接受由

定义的 JSON 字符串
@Consumes("application/json")

所以应该是这个方法

public String guardarDato(String jsonObject) {

您需要添加 Gson 作为服务器的依赖项,然后您应该能够将 JSON 获取到具有类似 PostParams params = Gson.fromJson(jsonObject, PostParams.class) 的对象,假设数据是从客户端正确发送的,但如前所述,您的 POJO 不会在 JsonObject 或普通 HashMap 之上添加太多功能

【讨论】:

  • 是的,我可以做到这一点,它的工作原理。但我需要使用: public String guardarDato(PostParams jsonObject) { It is possible?
  • 你可以在方法中使用PostParams params = Gson.fromJson(jsonObject)。我认为除了字符串或 int 之外,您不能将任何其他内容作为方法的参数。
  • 另外,我刚刚确实看到提到了Android,所以使用localhost确实需要替换为Web服务器的IP地址。
  • 是的,我在代码中为我的 Ip 更改了 localhost。问题是我正在迁移到 HttpUrlConnection 并且所有 Web 服务都已创建:@Consumes("application/json") public String guardarDato(PostParams jsonObject).
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多