在JAX-RS 2.0中将Jackson配置为JSON提供程序 [英] Configure Jackson as JSON Provider in JAX-RS 2.0
问题描述
我想将Jackson用作JAX-RS 2.0 Web服务的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>
。我怎样才能在带有Jersey 2.0的Jax-RS 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 = 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 Jackson JAX-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屋!