问题描述
我正在运行Jersey REST服务。代表我的资源的POJO是JAXB(XML)带注释的简单Java类(它们是从模式定义生成的 - 所以它们有注释)。
I am running a Jersey REST service. The POJO's which represent my resources are JAXB (XML) annotated simple Java classes (they are generated from a schema definition - so they have the annotations).
我想要Jersey /杰克逊忽略了XML-Annotations。我在我的web.xml中进行了此配置(如所述) ):
I want Jersey/Jackson to ignore the XML-Annotations. I did this configuration in my web.xml (as mentioned here):
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
我现在预计@ZMLElement注释将不再用于JSON字段命名策略。
I now expected that the @XMLElement annotation would not be used anymore for JSON field naming policy.
但是看看这个java字段(成员)
But looking at this java field (member)
@XmlElement(name = "person", required = true)
protected List<Person> persons;
我仍然得到以下JSON表示:
I still get the following JSON representation:
....,"person":[{"name":"FooBar", ....... (person without the 's')
所有其他字段仍然从@XmlElement注释中获取其JSON名称,而不是从Java字段名称获取。
All other fields also still get their JSON names from the @XmlElement annotation instead from the Java field name.
我想实现一个JSON输出,如杰克逊所描述的。
I would like to achieve a JSON output as describe in the Jackson Full Data Binding (POJO) Example.
它在这样的简单测试中工作正常(使用我的XML注释类):
It works fine in simple tests like this (with my XML annotated classes):
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(System.out, myObject);
但是嵌入在Jersey中我没有得到预期的JSON输出。
but embedded in Jersey I did not get the expected JSON output.
他们在Jersey的其他配置选项是否能获得'简单'的POJO JSON表示(因为这最适合需要反序列化JSON结果的客户端)。
Are their other configuration options in Jersey to get the 'simple' POJO JSON representation (because this fits best to clients which have to deserialize the JSON result).
谢谢克劳斯
详细解决方案
(1)实施 ContextResolver
for Jacksons ObjectMapper
,它创建一个不使用注释的ObjectMapper。
(1) Implement a ContextResolver
for Jacksons ObjectMapper
which creates an ObjectMapper that will not use annotations.
package foo.bar.jackson;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
/**
* Customized {@code ContextResolver} implementation that does not use any
* annotations to produce/resolve JSON field names.
*/
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonContextResolver implements ContextResolver<ObjectMapper> {
private ObjectMapper objectMapper;
/**
* Creates a new instance.
*
* @throws Exception
*/
public JacksonContextResolver() throws Exception {
this.objectMapper = new ObjectMapper().configure(
DeserializationConfig.Feature.USE_ANNOTATIONS, false)
.configure(SerializationConfig.Feature.USE_ANNOTATIONS, false);
;
}
/**
* @see javax.ws.rs.ext.ContextResolver#getContext(java.lang.Class)
*/
public ObjectMapper getContext(Class<?> objectType) {
return objectMapper;
}
}
(2)在应用程序中注册ContextResolver Spring bean .xml
(2) Register the ContextResolver Spring bean in your application.xml
<bean class="foo.bar.jackson.JacksonContextResolver"/>
推荐答案
在低级别,需要做些什么才能确保ObjectMapper不使用JAXBAnnotationIntrospector,而只使用默认的JacksonAnnotationIntrospector。我认为你应该能够构建ObjectMapper(默认情况下不添加JAXB introspector),并通过标准的JAX-RS提供程序机制注册它。这应该覆盖POJO映射器功能否则将构造的ObjectMapper。
At low level, what is needed to make sure that ObjectMapper does NOT use JAXBAnnotationIntrospector, but only default JacksonAnnotationIntrospector. I think you should be able to just construct ObjectMapper (which does not add JAXB introspector by default), and register it via standard JAX-RS provider mechanism. This should override ObjectMapper that POJO mapper functionality would otherwise construct.
这篇关于将Jersey / Jackson配置为不使用@XmlElement字段注释进行JSON字段命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!