java 将 Jersey/Jackson 配置为不使用 @XmlElement 字段注释进行 JSON 字段命名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5005668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:01:28  来源:igfitidea点击:

Configure Jersey/Hymanson to NOT use @XmlElement field annotation for JSON field naming

javajsonjerseyHymanson

提问by FrVaBe

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 REST 服务。代表我的资源的 POJO 是 JAXB (XML) 注释的简单 Java 类(它们是从模式定义生成的 - 因此它们具有注释)。

I want Jersey/Hymanson to ignore the XML-Annotations. I did this configuration in my web.xml (as mentioned here):

我希望 Jersey/Hymanson 忽略 XML 注释。我在我的 web.xml 中做了这个配置(正如这里提到的):

  <init-param>
    <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
    <param-value>true</param-value>
  </init-param>

I now expected that the @XMLElement annotation would not be used anymore for JSON field naming policy.

我现在希望 @XMLElement 注释不再用于 JSON 字段命名策略。

But looking at this java field (member)

但是看着这个java字段(成员)

@XmlElement(name = "person", required = true)
protected List<Person> persons;

I still get the following JSON representation:

我仍然得到以下 JSON 表示:

....,"person":[{"name":"FooBar", ....... (person without the 's')

All other fields also still get their JSON names from the @XmlElement annotation instead from the Java field name.

所有其他字段也仍然从 @XmlElement 注释而不是从 Java 字段名称中获取它们的 JSON 名称。

I would like to achieve a JSON output as describe in the Hymanson Full Data Binding (POJO) Example.

我想实现Hyman逊完整数据绑定 (POJO) 示例中描述的 JSON 输出。

It works fine in simple tests like this (with my XML annotated classes):

它在这样的简单测试中运行良好(使用我的 XML 注释类):

  ObjectMapper mapper = new ObjectMapper(); 
  mapper.writeValue(System.out, myObject);

but embedded in Jersey I did not get the expected JSON output.

但嵌入在 Je​​rsey 中我没有得到预期的 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).

他们在 Jersey 中的其他配置选项是否可以获得“简单”的 POJO JSON 表示(因为这最适合必须反序列化 JSON 结果的客户端)。

Thanks Klaus

谢谢克劳斯

Detailed solution

详细解决方案

(1) Implement a ContextResolverfor Hymansons ObjectMapperwhich creates an ObjectMapper that will not use annotations.

(1)ContextResolver为 Hymansons实现 a ,ObjectMapper它创建了一个不使用注释的 ObjectMapper。

package foo.bar.Hymanson;

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.Hymanson.map.DeserializationConfig;
import org.codehaus.Hymanson.map.ObjectMapper;
import org.codehaus.Hymanson.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 HymansonContextResolver implements ContextResolver<ObjectMapper> {

    private ObjectMapper objectMapper;

    /**
     * Creates a new instance.
     * 
     * @throws Exception
     */
    public HymansonContextResolver() 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) Register the ContextResolver Spring bean in your application.xml

(2)在你的application.xml中注册ContextResolver Spring bean

<bean class="foo.bar.Hymanson.HymansonContextResolver"/>

采纳答案by StaxMan

At low level, what is needed to make sure that ObjectMapper does NOT use JAXBAnnotationIntrospector, but only default HymansonAnnotationIntrospector. 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.

在低级别,需要什么来确保 ObjectMapper 不使用 JAXBAnnotationIntrospector,而只使用默认的 HymansonAnnotationIntrospector。我认为您应该能够构建 ObjectMapper(默认情况下不添加 JAXB 内省器),并通过标准 JAX-RS 提供程序机制注册它。这应该覆盖 POJO 映射器功能否则会构造的 ObjectMapper。