Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrading from Jersey 1.* to Jersey 2.5

I have decided to move to Jersey 2.5 and I am facing certain issues in Java Beans conversion to JSON. I have a User Bean and from one service I am trying to return a list of users.

My service code which fetches the list of users:

@GET
@Path("getAllUsers")
@Produces(MediaType.APPLICATION_JSON)
public List<User> getAllUsers() {
    //Fetching users list here
    return users;
}

I get following exception of server console:

[org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<com.example.User>.] with root cause
org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=java.util.List<com.example.User>.
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:227)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
    at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1139)
    at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:574)
    at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:381)
    at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:371)
    at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:262)

Am I missing any dependencies or any 2.5 specific configuration?

like image 878
Niranjan Borawake Avatar asked Jan 10 '14 08:01

Niranjan Borawake


1 Answers

Since you're using Jackson 1.9.x to (un)marshall JSON, make sure you have jersey-media-json-jackson module in your dependency list and that you're registering JacksonFeature in your application, i.e.:

public class MyResourceConfig extends ResourceConfig {

    public MyResourceConfig() {
        // ... other registrations ...

        register(JacksonFeature.class);
    }
}

To see more ways how to register providers in Jersey 2 refer to this article.

like image 79
Michal Gajdos Avatar answered Sep 30 '22 04:09

Michal Gajdos