简体   繁体   中英

Jersey throwing “MessageBodyWriter not found” exception on every GET request?

I have a simple entity that I'm attempting to return using Jersey 2.x and Jackson 2.x for JSON encoding.

However when I hit the endpoint in the browser for a GET request, it fails with:

HTTP Status 500 - org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class com.package.ResponseWrapper, genericType=class com.package.ResponseWrapper.

I suspect this is due to some lacking dependency or versions not matching. I tried several but still I get the same issue.

I'm using Maven, and here is how the jersey-related dependencies look like:

    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-common</artifactId>
        <version>2.9.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-entity-filtering</artifactId>
        <version>2.9.1</version>
    </dependency>

    <dependency> 
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>2.4.1</version>
    </dependency>

I read some folks fixing this by adding the follow dependency:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-json</artifactId>
    <version>1.8</version>
</dependency>

...but since I'm on Jersey 2.x and using Jackson's provider ( jackson-jaxrs-json-provider ) I don't think that is the way to go for me.

I'm not sure how to proceed here.. anyone has any ideas on what could I be missing?

Thank you!!

Ok, found the issue...

I forgot to add

packages("com.fasterxml.jackson.jaxrs.json");

to my ResourceConfig class.

Now everything is working!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM