在JavaEE应用程序中使用Jackson作为JAXB-JSON处理器 [英] Use Jackson as JAXB-JSON-processor in JavaEE Application

查看:872
本文介绍了在JavaEE应用程序中使用Jackson作为JAXB-JSON处理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过很多关于此事的文章和SO问题 - 但我只是没有让它发挥作用。
我的目标是在JavaEE应用程序中使用Jackson作为JSON处理器。到目前为止我有什么?

I've seen many articles and SO-questions about this - but I just don't get it working. My goal is to use Jackson as JSON processor in a JavaEE application. What do I have so far?


  • 这一个

  • either this one

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.17</version>
</dependency>


  • 还是这一个(到底哪一个是正确的?)

  • or this one (which of them is correct in the end?)

    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.5.2</version>
    </dependency>
    


  • 加上这个(由于这篇文章,因为jackson包中不再存在自动发现):

  • plus this (due to this article, because auto discovery shall not exist in jackson packages anymore):

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-metainf-services</artifactId>
        <version>2.17</version>
    </dependency>
    


  • 简单REST注册:

    <servlet>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>javax.ws.rs.core.Application</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    



    一个简单的对象



    A simple Object

    import com.fasterxml.jackson.annotation.JsonProperty;
    
    public class Dummy {
        private String name;
    
        @JsonProperty("username")
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    }
    



    REST资源



    The REST resource

    @GET
    @Path("test")
    @Produces(MediaType.APPLICATION_JSON)
    public Response getRequest() {
        Dummy dummy = new Dummy();
        dummy.setName("rolf");
    
        return Response.ok(dummy).build();
    }
    






    输出为


    And the output is

    {"name":"rolf"}
    

    而不是预期的

    {"username":"rolf"}
    

    更新

    我正在使用GlassFish应用程序服务器。

    I'm using the GlassFish application server.

    推荐答案

    我的猜测是你在Glassfish上,它使用MOXy作为默认值JSON提供者。您可以使用< init-param> 停用它。

    My guess would be you're on Glassfish, which uses MOXy as its default JSON provider. You can disable it with an <init-param>.

    <init-param>
        <param-name>jersey.config.server.disableMoxyJson</param-name>
        <param-value>true</param-value>
    </init-param>
    

    jersey-media-json-jackson 具有可自动发现的功能,应自动注册。我不确定Glassfish的自动发现功能,以及内部可能使用的较低版本的Jersey,如果它不会被注册。但无论哪种方式,您为web.xml配置的方式是启用类路径扫描,因此无论如何都应该选择杰克逊提供商。

    The jersey-media-json-jackson has an auto-discoverable feature that should automatically register it. I'm not sure about the auto-discoverable feature in the case of Glassfish, and possible lower version of Jersey it uses internally, and if it will cause it not to be register. But either way, the way you have configured you web.xml is to enable classpath scanning, so the Jackson provider should be picked up anyway.

    一些FYI


    • jersey-media-json-jackson 实际使用杰克逊 - JAXRS-JSON-提供商。它只是将它包装在 JacksonFeature 中,并启用它自动发现。

    • 如果它仍然不起作用,您可以尝试创建一个功能来处理注册和禁用。例如

    • jersey-media-json-jackson actually uses jackson-jaxrs-json-provider. It just wraps it in a JacksonFeature, and enables auto-discovery of it.
    • If it still doesn't work, you can try to create a feature to handle the registering and disabling. For example

    @Provider
    public class JsonFeature implements Feature {
        @Override
        public boolean configure(FeatureContext context) {
            context.property("jersey.config.server.disableMoxyJson", true);
            // this is in jersey-media-json-jackson
            context.register(JacksonFeature.class);
    
            // or from jackson-jaxrs-json-provider
            context.register(JacksonJsonProvider.class);
            // for JAXB annotation support
            context.register(JacksonJaxbJsonProvider.class);
    
            return true;
        }
    }
    


    这篇关于在JavaEE应用程序中使用Jackson作为JAXB-JSON处理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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