Jackson's JsonNode with JAX-RS

送分小仙女□ 提交于 2020-08-07 06:09:20

问题


I'm writing a JAX-RS web service that returns JSON back to the client, and I'm trying to use the org.codehaus.jackson libraries for dealing with JSON objects. The problem I'm having is that my JsonNode isn't getting properly serialized before it is returned to the client. There are a bunch of extraneous properties in the response. I would like the simplest JSON representation of the JsonNode to be returned.

Here's a (contrived) example:

@GET
@Path("user")
@Produces(MediaType.APPLICATION_JSON)
public JsonNode getUser() {
    ObjectNode node = JsonNodeFactory.instance.objectNode();
    node.put("user", "jDoe");
    return node;
}

The JSON response from this is pretty ugly:

{"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}

I would prefer it to be simply

{"user":"jDoe"}

Am I missing something? I don't understand why there would be problems serializing a simple JsonNode into JSON.

For what it's worth, returning a JSONObject from the JSON4J libraries works as expected, but I would prefer using Jackson's libraries.


回答1:


This should work as-is, so I am suspecting that maybe there is a version incompatibility. Since Jersey uses Jackson 1.x (1.8 or 1.9), you need to be using same version; 2.0 classes are in different Java packages, to allow 1.x and 2.x to co-exist (to avoid forcing upgrade).

So make sure you are using same version as Jersey does.



来源:https://stackoverflow.com/questions/12593933/jacksons-jsonnode-with-jax-rs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!