我有一个运行 Jersey 2.8 的应用程序,我正在尝试将其升级到 2.22.2。但是当我这样做时, Jersey 不断抛出这些异常:
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList.
我什至手动添加了 jackson 依赖项
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.2</version>
</dependency>
但这并没有帮助。
我正在使用 Spring Boot,这是我的配置类:
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
packages("com.xxxxxx.resource", "com.xxxxx.provider");
register(MultiPartFeature.class);
register(LoggingFilter.class);
this.property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, true);
this.property(ServerProperties.RESPONSE_SET_STATUS_OVER_SEND_ERROR, true);
this.property(ServletProperties.FILTER_FORWARD_ON_404, true);
}
}
以及我的 pom.xml 的相关部分:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey</groupId>
<artifactId>jersey-bom</artifactId>
<version>${jersey.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.2</version>
</dependency>
最佳答案
你需要的不仅仅是Jackson(核心),它只是一个序列化框架,但没有提供任何绑定(bind)到Jersey框架的机制。对于序列化,Jersey 使用 MessageBodyReader
和 MessageBodyWriter
。有一个 Jackson 模块,它采用核心 Jackson 并使用它为 JAX-RS 的 JSON 创建 MessageBodyReader
和 MessageBodyWriter
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson2.version}</version>
</dependency>
Jersey 有一个模块可以向该库添加更多内容,并进行一些自动注册。那个 Artifact 是
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>${jersey2.version}</version>
</dependency>
如果您正在使用 Jersey,那么您应该使用这个。添加后您无需执行任何其他操作。它会自动注册 Jackson 提供商
请注意,jersey-media-json-jackson
会提取所有必需的 Jackson 库,但它不会是 Jackson 的最新版本。对于 Jersey 2.22.2,我认为 Jackson 版本是 2.5(不是 100% 确定)。如果您需要最新的功能,您应该排除所有 Jackson 库,并添加此答案中的第一个库以及您想要的版本以及上面的版本。这些是您应该对“jersey-media-json-jackson”进行的排除
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-base</artifactId>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
关于java - 将 Jersey 更新到 2.22.2 时出现 MessageBodyProviderNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35992471/