我正在编写一个JAX-RS Web服务,它将JSON返回给客户端,我正在尝试使用org.codehaus.jackson
库来处理JSON对象。我遇到的问题是我的JsonNode
在返回客户端之前没有正确序列化。响应中有许多无关的属性。我希望返回JsonNode
最简单的JSON表示。
这是一个(人为的)例子:
@GET
@Path("user")
@Produces(MediaType.APPLICATION_JSON)
public JsonNode getUser() {
ObjectNode node = JsonNodeFactory.instance.objectNode();
node.put("user", "jDoe");
return node;
}
JSON对此的回应非常难看:
{"object":true,"elements":[{"textual":true,"textValue":"jDoe","binaryValue":"jDoe","valueAsText":"jDoe","valueNode":true,"containerNode":false,"missingNode":false,"array":false,"object":false,"pojo":false,"number":false,"integralNumber":false,"floatingPointNumber":false,"int":false,"long":false,"double":false,"bigDecimal":false,"bigInteger":false,"boolean":false,"null":false,"binary":false,"booleanValue":false,"intValue":0,"longValue":0,"doubleValue":0.0,"decimalValue":0,"bigIntegerValue":0,"valueAsInt":0,"valueAsLong":0,"valueAsDouble":0.0,"valueAsBoolean":false,"elements":[],"fieldNames":[],"fields":[]}],"fieldNames":["user"],"fields":[{"key":"user","value":{"textual":true,"textValue":"jDoe","binaryValue":"jDoe","valueAsText":"jDoe","valueNode":true,"containerNode":false,"missingNode":false,"array":false,"object":false,"pojo":false,"number":false,"integralNumber":false,"floatingPointNumber":false,"int":false,"long":false,"double":false,"bigDecimal":false,"bigInteger":false,"boolean":false,"null":false,"binary":false,"booleanValue":false,"intValue":0,"longValue":0,"doubleValue":0.0,"decimalValue":0,"bigIntegerValue":0,"valueAsInt":0,"valueAsLong":0,"valueAsDouble":0.0,"valueAsBoolean":false,"elements":[],"fieldNames":[],"fields":[]}}],"containerNode":true,"valueNode":false,"missingNode":false,"array":false,"pojo":false,"number":false,"integralNumber":false,"floatingPointNumber":false,"int":false,"long":false,"double":false,"bigDecimal":false,"bigInteger":false,"textual":false,"boolean":false,"null":false,"binary":false,"booleanValue":false,"intValue":0,"longValue":0,"doubleValue":0.0,"decimalValue":0,"bigIntegerValue":0,"valueAsInt":0,"valueAsLong":0,"valueAsDouble":0.0,"valueAsBoolean":false}
我希望它只是
{"user":"jDoe"}
我错过了什么吗?我不明白为什么将一个简单的JsonNode序列化为JSON会出现问题。
对于它的价值,从JSON4J库返回JSONObject
按预期工作,但我更喜欢使用Jackson的库。
答案 0 :(得分:3)
这应该按原样运行,所以我怀疑可能存在版本不兼容。 由于Jersey使用Jackson 1.x(1.8或1.9),你需要使用相同的版本; 2.0类在不同的Java包中,允许1.x和2.x共存(以避免强制升级)。
因此,请确保您使用的是与泽西相同的版本。