2

I'm using the latest version of Jersey to implement some REST sample services.
Do you have any clue why when I generate the following HTTP request using Fiddler,I get:
500 Internal Server Error

MessageBodyWriter not found for media type={application/json, q=1000}, type=class java.util.ArrayList, genericType=java.util.ArrayList<com.example.Todo>

HTTP request:

GET http://localhost:8080/RestProject/rest/todos/1 HTTP/1.1
Connection: close
Accept: application/json
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/41.0.2272.101 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: it-IT,it;q=0.8,en-US;q=0.6,en;q=0.4
Host: localhost:8080

That's the Rest method getting called:

 //This method is called if XML or JSON is requested  
  @GET  
  @Path("{id}")  
  @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON,MediaType.TEXT_XML})  
  public Todo getEntityXMLOrJSON(@PathParam("id") int id)  
  {
    Todo todo = new Todo();
    todo.setSummary("This is my first todo " + id);
    todo.setDescription("This is my first todo");
    return todo;
  }

When I request xml data everything works fine.

EDIT:

enter image description here

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
GionJh
  • 2,742
  • 2
  • 29
  • 68
  • 1
    I think that this should help: http://stackoverflow.com/questions/26518996/jersey-json-serialization – bbastu Mar 28 '15 at 10:30
  • 1
    Do you have any stacktrace you can show us. Generally a 500 will mean an exception is thrown an in most cases is logged – Paul Samsotha Mar 28 '15 at 10:57
  • 1
    Also can you list all your dependencies/jars – Paul Samsotha Mar 28 '15 at 10:58
  • yes:MessageBodyWriter not found for media type={application/json, q=1000}, type=class java.util.ArrayList, genericType=java.util.ArrayList. – GionJh Mar 28 '15 at 10:58
  • The exact same exception is raised when you register the JacksonJsonProvider? This would look like it has not been recongized by jersey then. – bbastu Mar 28 '15 at 11:05
  • 1
    Keep only the jars you saw in the tutorial. Take everything else out. Then find and add [these](http://stackoverflow.com/a/29136934/2587435). It should work. – Paul Samsotha Mar 28 '15 at 11:07
  • 1
    And are you sure this is the code that corresponds to the error you're showing us? Because the error is about trying to marshal an `ArrayList`. So simple have a `Todo` you are returning. – Paul Samsotha Mar 28 '15 at 11:09
  • still not genereting json...but now i get java.lang.ClassNotFoundException: com.fasterxml.jackson.annotation.JsonAutoDetect – GionJh Mar 28 '15 at 11:22
  • thanks, @peeskillet now it works fine,could you make your comments an answer so that I can accept ? – GionJh Mar 28 '15 at 11:29

2 Answers2

2

I'm pretty familiar with the Vogella tutorial. So many people have posted about it. It uses Jersey 2. You are trying to add some Jersey 1 jars to the project. Take all the ones you added on your own out. Then find and add these

enter image description here

And if you're using web.xml, then register the provider like so

<param-name>jersey.config.server.provider.packages</param-name>
<param-value>
    com.jersey.jaxb,com.fasterxml.jackson.jaxrs.json
</param-value>

adding an init param to the jersey servlet. Otherwise, using Java config, in your ResourceConfig, just

register(JacksonJaxbJsonProvider.class);

That's for JAXB annotation support. If you don't need it, then you can just use JacksonJsonProvider

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • If I use jersey 2 then all these jackson jars would not be needed, right ? – GionJh Mar 28 '15 at 11:38
  • 1
    You _are_ using Jersey 2. The main thing you need a is a provider, which is the `jackson-jaxrs-json-provider`. But that jar is dependent on all the other jars. These are basic Jackson jars. The provider can't live on its own – Paul Samsotha Mar 28 '15 at 11:40
  • 1
    With Jersey 1, would use the `jersey-json` you tried to add. But even that is dependent on some Jackson jars, which you do not have – Paul Samsotha Mar 28 '15 at 11:42
0

My solution was adding the following dependencies;

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>your jersey version</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-json-jackson</artifactId>
    <version>your jersey version</version>
</dependency>
techspider
  • 3,370
  • 13
  • 37
  • 61
Laazo
  • 467
  • 8
  • 22