我有這個簡單的代碼:使用的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