user384729
user384729

Reputation: 403

MessageBodyProviderNotFoundException when updating Jersey to 2.22.2

I have a working app with Jersey 2.8, that I'm trying to upgrade to 2.22.2. But when I do that, Jersey keeps throwing these exceptions:

org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList.

I even manually added the jackson dependency

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.7.2</version>
</dependency>

But it doesn't help.

I'm using Spring Boot and this is my config class:

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);
    }

}

And the relevant parts of my 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>

Upvotes: 2

Views: 1404

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209112

You need more than just Jackson (core), which is just a serialization framework, but but does not provided any mechanism to bind into the Jersey framework. For serialization, Jersey uses MessageBodyReaders and MessageBodyWriters. There is a Jackson module that takes core Jackson and uses it to create a MessageBodyReader and MessageBodyWriter for JSON for JAX-RS

<dependency>
    <groupId>com.fasterxml.jackson.jaxrs</groupId>
    <artifactId>jackson-jaxrs-json-provider</artifactId>
    <version>${jackson2.version}</version>
</dependency>

Jersey has a module that add some more to that library, and does some auto registration. That artifact is

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>${jersey2.version}</version>
</dependency>

This is the one you should use, if you are working with Jersey. You don't need to do anything else after you add it. It automatically register the Jackson providers

Note that the jersey-media-json-jackson pulls in all the required Jackson libraries, but it will not be latest version of Jackson. With Jersey 2.22.2, I think the Jackson version is 2.5 (not 100% sure). If you need the latest features, you should exclude all the Jackson libraries, and add the first one in this answer, with the version you want, along with the above one. These are the exclusions you should make to `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>

Upvotes: 2

Related Questions