我在調用Web服務調用時收到MessageBodyWriter錯誤。Jersey REST錯誤,未找到媒體類型= application/json的MessageBodyWriter
我在我的classpath中包含genson-1.1.jar
,我相信這是假設包含MessageBodyWriter實現。但我得到這個錯誤。
我正在使用澤西島2.13。
我也試過了,沒有註冊「GensonJsonConverter.class」,仍然給出了同樣的錯誤。
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ClientConfig config = new ClientConfig();
WebTarget target = client.target("http://localhost:8080/WebServices/rest/hello");
target.path("Lion King");
target.register(GensonJsonConverter.class);
String response = target.request().accept(MediaType.APPLICATION_JSON).get(String.class);
resp.getWriter().print(response);
}
@Path("hello")
public class Hello {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Book getThisBook(@PathParam("bookName") String bookName){
return new Book(bookName, "Msn", new Date());
}
}
web.xml中的堆棧跟蹤
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>WebServices</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey Rest Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.webservices</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>com.webservices.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Rest Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
</web-app>
部分:
SEVERE: MessageBodyWriter not found for media type=application/json, type=class com.webservices.entity.Book, genericType=class com.webservices.entity.Book.
Oct 10, 2014 3:19:49 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [myServlet] in context with path [/WebServices] threw exception
javax.ws.rs.InternalServerErrorException: HTTP 500 Internal Server Error
發佈您的web.xml文件 – brso05 2014-10-10 20:48:24
@ brso05 web.xml發佈 – Susie 2014-10-10 20:50:14
我使用球衣1.18我認爲我也有更新版本的球衣的問題。您可能想查看1.18 – brso05 2014-10-10 20:54:28