【问题标题】:How to add JSON Support in Jersey 2.x如何在 Jersey 2.x 中添加 JSON 支持
【发布时间】:2014-08-04 10:48:43
【问题描述】:

我已将我的应用程序从 Jersey 1.x 迁移到 2.x,我正在尝试从 web.xml 替换以下条目

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

为了能够以 json 格式获取实体。

我已阅读官方文档,我必须通过添加 jersey-media-json-jackson 依赖项来使用 Jackson 提供程序并注册 JacksonFeature。

我添加了以下依赖:

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

但现在我不知道我必须在我的 web.xml 文件中添加什么作为

中的值

&lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;

有什么想法吗?我不想通过 web.xml 之类的配置文件而不是在我的代码中执行此操作。

【问题讨论】:

  • 我不知道您可以将 Jersey 配置为仅使用 XML 使用 Jackson。 Jersey 文档 (jersey.java.net/documentation/latest/media.html#json.jackson) 表明您需要实现 ContextResolver 以返回适当的 ObjectMapper 供 Jersey 使用。您可以尝试添加对 jersey-media-json-jackson 的依赖项,并确保您的类路径中没有任何 `MOXy 内容。
  • @Baldy 好的,但是我如何注册实现上下文解析器的类?
  • 你找到答案了吗?

标签: java json jersey


【解决方案1】:

我的配置如下:

    <servlet>
        <servlet-name>REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.<your-company-name-here></param-value>
        </init-param>
        <init-param>
            <param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

【讨论】:

    【解决方案2】:

    我使用ResourceConfig 类注册所有内容。我还找到了描述如何以其他方式进行操作的资源 (http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/)。

    @ApplicationPath("/ws")
    public class WsResourceConfig extends ResourceConfig {
    
        public WsResourceConfig() {
            register(Jackson2Feature.class);
            register(ObjectMapperResolver.class);
            //register(MultiPartFeature.class); // if needed
            //register(new LoggingFilter(LOGGER, true)); // to enable logging
    
            //register(my resources...); to register my service classes
        }
    
        private static class ObjectMapperResolver implements ContextResolver<ObjectMapper> {
            private static ObjectMapper mapper = new ObjectMapper();
            static {
                // configure mapper here
            }
            @Override
            public ObjectMapper getContext(Class<?> type) {
                return mapper;
            }
        }
    
        /**
         * Feature to disable Moxy and Enable Jackson processing
         */
        private static class Jackson2Feature implements Feature {
    
            @Override
            public boolean configure(FeatureContext context) {
                final String disableMoxy = PropertiesHelper.getPropertyNameForRuntime(
                        CommonProperties.MOXY_JSON_FEATURE_DISABLE,
                        context.getConfiguration().getRuntimeType());
                context.property(disableMoxy, true);
    
                // add the default Jackson exception mappers and allow jaxb annotations
                context.register(JsonParseExceptionMapper.class);
                context.register(JacksonJaxbJsonProvider.class, MessageBodyReader.class, MessageBodyWriter.class);
                return true;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-01
      • 2015-12-19
      • 1970-01-01
      • 2011-07-06
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      相关资源
      最近更新 更多