我想将Jackson作为我的JAX-RS 2.0 Web服务的JSON提供程序。对于JAX-RS,我在GlassFish 4中使用Jersey 2.0。对于JAX-RS 1.x,我可以添加
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
在Jax-RS 2.0和Jersey 2.0中,我该如何在我的web.xml
中实现此功能?我像这样实现一个应用程序类。public class MyRESTExampleApplication extends ResourceConfig {
public MyRESTExampleApplication() {
packages("com.carano.fleet4all.restExample");
register(JacksonFeature.class);
}
}
并将这些行添加到我的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 ...我的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>