[英]Read JSON with Jersey 2.0 (JAX-RS 2.0)
I was using Jersey 1.16
to consume a JSON
, but now I'm with difficulties to consume a JSON using Jersey 2.0 (that implements JAX-RS 2.0). 我使用Jersey 1.16
来使用JSON
,但是现在我很难使用Jersey 2.0(实现JAX-RS 2.0)来使用JSON。
I have a JSON response like this: 我有这样的JSON响应:
{
"id": 105430,
"version": 0,
"cpf": "55443946447",
"email": "maria@teste.br",
"name": "Maria",
}
and the method that consumes it: 以及使用它的方法:
public static JSONObject get() {
String url = "http://127.0.0.1:8080/core/api/person";
URI uri = URI.create(url);
final Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(uri);
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
if (response.getStatus() == 200) {
return response.readEntity(JSONObject.class);
}
}
I also tried: 我也尝试过:
return webTarget.request(MediaType.APPLICATION_JSON).get(JSONObject.class);
But the jSONObject return is null . 但是jSONObject返回为null 。 I don't understand my error because the response is OK! 我不明白我的错误,因为响应正常!
I have found the solution. 我找到了解决方案。 Maybe it is not the best of, but it works. 也许这不是最好的方法,但它确实有效。
public static JsonObject get() {
String url = "http://127.0.0.1:8080/core/api/person";
URI uri = URI.create(url);
final Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(uri);
Response response = webTarget.request(MediaType.APPLICATION_JSON).get();
//Se Response.Status.OK;
if (response.getStatus() == 200) {
StringReader stringReader = new StringReader(webTarget.request(MediaType.APPLICATION_JSON).get(String.class));
try (JsonReader jsonReader = Json.createReader(stringReader)) {
return jsonReader.readObject();
}
}
return null;
} }
I switched the class JSONObject (package import org.codehaus.jettison) by JsonObject (package javax.json) and I used the methods to manipulate the content as String. 我通过JsonObject(包javax.json包)切换了JSONObject类(包导入org.codehaus.jettison包),并使用这些方法将内容作为String进行操作。
S. S.
This is how to use the Response type correctly: 这是正确使用Response类型的方法:
private void getRequest() {
Client client = ClientBuilder.newClient();
String url = "http://localhost:8080/api/masterdataattributes";
WebTarget target = client.target(url);
Response res = target
.request(MediaType.APPLICATION_JSON)
.get();
int status = res.getStatus();
String json = res.readEntity(String.class);
System.out.println(String.format("Status: %d, JSON Payload: %s", status, json));
}
If you're just interested in the payload, you could also just issue a get(String.class). 如果您仅对有效负载感兴趣,则还可以发出get(String.class)。 But usually you will also want to check the response status, so working with the Response is usually the way to go. 但是通常您还需要检查响应状态,因此使用响应通常是可行的方法。
If you want a typed (generic) JSON response, you could also have readEntity return a Map, or a list of Map if the response is an array of objects as in this example: 如果您需要类型化的(通用)JSON响应,则也可以让readEntity返回一个Map,或者如果响应是对象数组,则返回Map列表,如以下示例所示:
List<Map<String, Object>> json = res.readEntity(new GenericType<List<Map<String, Object>>>() {});
String id = (String) json.get(0).get("id");
System.out.println(id);
mmey答案是正确和最佳的答案,而不是两次调用服务,而是一次执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.