【发布时间】:2016-09-07 04:38:21
【问题描述】:
我有这个简单的代码:
package com.example
import javax.json.Json;
import javax.json.JsonObject;
...
@Path("/")
public class Resource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response defaultEntry() {
JsonObject result = (Json.createObjectBuilder()
.add("hello", "world")
.build());
return Response.status(200).entity(result.toString()).build();
}
}
我是 Java 新手,谁能解释一下原因,如果我省略了对 result.toString() 的调用,而只是将 result 传递给 .entity(就像这样:return Response.status(200).entity(result).build()),我会在客户端得到 JSON包括类型信息等,但不是我所期望的:
{"hello":{"chars":"world","string":"world","valueType":"STRING"}}
这样做的目的是什么?将JsonObject 传递给它与传递字符串有何不同?
另外,我在文档中没有找到Response.entity 方法(https://jersey.java.net/apidocs/2.11/jersey/javax/ws/rs/core/Response.html)。我从教程中复制了这段代码,但没有正确解释发生了什么......
【问题讨论】:
-
你在期待什么?只有价值观?
-
您知道您使用的是哪个 JSON 序列化程序吗? Moxy、Jackson、Gson 等...?
-
我期待
{"hello": "world"}。我不知道我使用的是哪个序列化程序。如何发现?
标签: java jersey-2.0