Reputation: 2885
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.
Upvotes: 5
Views: 3944
Reputation: 116512
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.
Upvotes: 3