我有这个简单的代码:使用的JSONObject作为实体泽西2响应
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,杰克逊,Gson等...? –
我期待'{“hello”:“world”}'。我不知道我正在使用哪个序列化程序。如何找出答案? – akonsu