在 JAX-RS 2.0 中将 Jackson 配置为 JSON 提供程序 [英] Configure Jackson as JSON Provider in JAX-RS 2.0

查看:20
本文介绍了在 JAX-RS 2.0 中将 Jackson 配置为 JSON 提供程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Jackson 作为我的 JAX-RS 2.0 网络服务的 JSON 提供程序.对于 JAX-RS,我在 GlassFish 4 中使用 Jersey 2.0.使用 JAX-RS 1.x,我可以添加

I want to use Jackson as JSON provider for my JAX-RS 2.0 webservice. For JAX-RS I use Jersey 2.0 in GlassFish 4. With JAX-RS 1.x I can add

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

在我的 web.xml 中.我如何在 Jax-RS 2.0 和 Jersey 2.0 中做到这一点?我实现了一个这样的应用程序类

in my web.xml. How can i do this in Jax-RS 2.0 with Jersey 2.0? I implement a application class like this

public class MyRESTExampleApplication extends ResourceConfig {
    public MyRESTExampleApplication() {
         packages("com.carano.fleet4all.restExample");
         register(JacksonFeature.class);
    }
}

并将这些行添加到我的 web.xml.

and add these lines to my web.xml.

<init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>com.example.restExample.MyRESTExampleApplication</param-value>
</init-param>

但是我得到了一个例外的请求org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class ...

But I get an exception by the request org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class ...

我的 pom.xml 看起来像这样

<dependencies>
  <dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
  </dependency>
  <dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
  </dependency>
</dependencies>

推荐答案

你应该只需要获取实现jar JacksonJAX-RS 提供者,将其添加到您的类路径中,它应该可以工作.2.x 版使用基于 SPI 的自动注册,因此您不需要 web.xml 中的任何内容.

You should only need to get the implementation jar Jackson JAX-RS provider, add that to your classpath, and it should work. Version 2.x uses SPI-based auto-registration, so that you do not need anything in web.xml.

这篇关于在 JAX-RS 2.0 中将 Jackson 配置为 JSON 提供程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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