Jersey REST错误,找不到媒体类型= application / json的MessageBodyWriter

时间:2014-10-10 20:45:21

标签: java web-services rest jersey jax-rs

调用Web服务调用时,我发现MessageBodyWriter未找到错误。

我在我的类路径中包含genson-1.1.jar,我认为它包含MessageBodyWriter实现。但我得到了这个错误。

我正在使用Jersey 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

My lib folder

1 个答案:

答案 0 :(得分:3)

是的,最新的泽西岛发布了它发现的方式&#34;扩展&#34;已经改变。 Genson很快就会更新,以便开箱即用。同时修复它:

1)添加对metainf-services的依赖

2)以编程方式注册。

请查看the documentation here有更详细的说明,并提供有关选择哪种解决方案的建议。

编辑由于您没有构建所有容器程序,因此您可以更轻松地使用metainf服务解决方案。