如何精确匹配 cxf 的 json 输出?
编辑:我很困惑——使用的是 cxf,而不是球衣。有没有一种方法可以将带注释的对象转换为类似于 Jackson 的 ObjectMapper 的 json?
原始消息:
嗨, 我们目前正在使用 jaxrs 将 Web 响应转换为 xml/json。然而,我现在想做的是使用 ObjectMapper(?) 在我的代码中生成等效的 json 字符串。
例如,给定一个控制器和 jaxb 注释的返回对象:
@Path("/foo")
@Produces({"application/json", "application/xml"})
public class FooController {
@GET
@Path("/some_action")
public TopDTO someAction(@QueryParam("arg") String arg) {
...
}
}
@XmlRootElement(name="topDTO")
@XmlAccessorType(XmlAccessType.NONE)
public class TopDTO {
...
@XmlAttribute(name="attr")
public String getAttr() {
return "blah";
}
@XmlElement(name="innerDTO")
public InnerDTO getInnerDTO() {
...
}
}
@XmlRootElement(name="innerDTO")
@XmlAccessorType(XmlAccessType.NONE)
public class InnerDTO {
...
}
点击 http://myserver/.../foo .json 输出一些漂亮的 json:
{"topDTO":{"@attr":"blah","innerDTO":...}}
现在我希望能够在内部生成确切的 json:
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
mapper.getSerializationConfig().setSerializationInclusion(Inclusion.ALWAYS);
mapper.getSerializationConfig().set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, false);
mapper.getSerializationConfig().set(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
return mapper.writeValueAsString(snapshotDTO);
但是,这似乎根本不起作用;大多数带注释的属性和元素都丢失了,这些属性没有像 jaxrs 输出那样以“@”为前缀,等等。
我是否缺少一些简单的东西? jaxrs 本身如何将带注释的对象转换为 json 字符串?
谢谢! 乔
Edit: i was confused-- were using cxf, not jersey. Is there a way to convert an annotated object to json that is similar to jackson's ObjectMapper?
original msg:
Hi,
We are currently using jaxrs to convert our web responses to xml/json. What I would like to do now, however, is generate an equivalent json string inside my code using ObjectMapper(?).
For instance, given a controller and jaxb-annotated return object:
@Path("/foo")
@Produces({"application/json", "application/xml"})
public class FooController {
@GET
@Path("/some_action")
public TopDTO someAction(@QueryParam("arg") String arg) {
...
}
}
@XmlRootElement(name="topDTO")
@XmlAccessorType(XmlAccessType.NONE)
public class TopDTO {
...
@XmlAttribute(name="attr")
public String getAttr() {
return "blah";
}
@XmlElement(name="innerDTO")
public InnerDTO getInnerDTO() {
...
}
}
@XmlRootElement(name="innerDTO")
@XmlAccessorType(XmlAccessType.NONE)
public class InnerDTO {
...
}
Hitting http://myserver/.../foo.json puts out some pretty json:
{"topDTO":{"@attr":"blah","innerDTO":...}}
Now I would like to be able to generate that exact json internally:
ObjectMapper mapper = new ObjectMapper();
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector();
mapper.getSerializationConfig().setAnnotationIntrospector(introspector);
mapper.getSerializationConfig().setSerializationInclusion(Inclusion.ALWAYS);
mapper.getSerializationConfig().set(SerializationConfig.Feature.AUTO_DETECT_FIELDS, false);
mapper.getSerializationConfig().set(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);
return mapper.writeValueAsString(snapshotDTO);
However, this doesn't seem to work at all; most of the annotated attributes and elements are missing, the attributes aren't prefixed with "@" as they are with jaxrs output, etc.
Am i missing something simple? How does jaxrs itself convert an annotated object to a json string?
Thanks!
joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 JSONJAXBContext 到 创建一个编组器并使用它来将对象序列化为 JSON 格式。我认为你不需要杰克逊。
Use the JSONJAXBContext to create a marshaller and use it to serialize your object to the JSON format. I don't think you need jackson.
实际上看起来您不仅使用了 CXF,而且没有使用 Jackson 的 json 序列化。为什么?因为 Jackson 不会在声明为属性的事物前面添加“@”。
如果您想使用 ObjectMapper,您可能想从默认值开始,并尝试根据您想要更改的内容来更改内容,而不是从一组配置覆盖开始(例如:禁用 getter/setter 自动检测) ,这似乎不是你应该做的事情)。
It actually looks like not only are you using CXF, you are not using Jackson's json serialization. Why? Because Jackson does not add '@' in front of things declared as attributes.
If you want to use ObjectMapper you probably want to start with defaults, and try to change things according to what you want to change, and not by starting with a set of configuration overrides (for example: you are disabling getter/setter auto-detection, which doesn't seem like something you should be doing).
是的。可以将 Jackson 设置为提供者,如 http://cxf.apache.org /docs/jax-rs-data-bindings.html
。我观察到杰克逊在很多方面都很简单、方便。
Yes. Can set Jackson as a provider as in http://cxf.apache.org/docs/jax-rs-data-bindings.html
. I have observed Jackson is simple and convenient in many ways.