带有JAX-RS的Jackson的JsonNode [英] Jackson's JsonNode with JAX-RS

查看:106
本文介绍了带有JAX-RS的Jackson的JsonNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个将JSON返回给客户端的JAX-RS Web服务,并且试图使用org.codehaus.jackson库来处理JSON对象.我遇到的问题是我的 JsonNode 在返回给客户端之前没有得到正确的序列化.响应中有很多无关紧要的属性.我希望返回JsonNode的最简单的JSON表示形式.

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;
}

由此产生的JSON响应非常难看:

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}

我希望它简单

{"user":"jDoe"}

我错过了什么吗?我不明白为什么将一个简单的JsonNode序列化为JSON会出现问题.

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.

推荐答案

这应该是按原样工作的,所以我怀疑可能存在版本不兼容的情况. 由于Jersey使用的是Jackson 1.x(1.8或1.9),因此您需要使用相同的版本. 2.0类位于不同的Java包中,以允许1.x和2.x共存(以避免强制升级).

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).

因此,请确保使用与Jersey相同的版本.

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

这篇关于带有JAX-RS的Jackson的JsonNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆