将 Jersey/Jackson 配置为不使用 @XmlElement 字段注释进行 JSON 字段命名 [英] Configure Jersey/Jackson to NOT use @XmlElement field annotation for JSON field naming

查看:28
本文介绍了将 Jersey/Jackson 配置为不使用 @XmlElement 字段注释进行 JSON 字段命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行 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/Jackson 忽略 XML 注释.我在我的 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>

我现在希望 @XMLElement 注释不再用于 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.

我想实现 Jackson 完整数据绑定 (POJO) 中描述的 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);

但是嵌入在 Je​​rsey 中我没有得到预期的 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) 为 Jacksons ObjectMapper 实现一个 ContextResolver,它创建了一个不使用注解的 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) 在 application.xml 中注册 ContextResolver Spring bean

(2) Register the ContextResolver Spring bean in your application.xml

<bean class="foo.bar.jackson.JacksonContextResolver"/>

推荐答案

在底层,需要什么来确保 ObjectMapper 不使用 JAXBAnnotationIntrospector,而只使用默认的 JacksonAnnotationIntrospector.我认为您应该能够构建 ObjectMapper(默认情况下不添加 JAXB 内省器),并通过标准 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 字段命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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