JSON not generated in using Jersey

て烟熏妆下的殇ゞ 提交于 2019-12-11 03:50:37

问题


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:


回答1:


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

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




回答2:


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>


来源:https://stackoverflow.com/questions/29316018/json-not-generated-in-using-jersey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!