休息JAX-RS异常,找不到MessageBodyWriter [英] Rest JAX-RS exception, MessageBodyWriter not found

查看:197
本文介绍了休息JAX-RS异常,找不到MessageBodyWriter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用泽西2.13

I am using Jersey 2.13

当我尝试通过浏览器中的网址访问资源时,我收到MessageBoddyWriter异常。

I get MessageBoddyWriter not found exception when I try to access a resource via a url in a browser.

例外:

MessageBodyWriter not found for media type=application/json, 
type=class java.util.ArrayList, genericType=java.util.List<com.webservices.entity.Book>.

我有另一种产生APPLICATION_XML的方法,似乎工作正常。

I have another method that produces "APPLICATION_XML" and that seems to work fine.

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Book { 
    @XmlElement
    private String name;    
    @XmlElement
    private String author;
    //getters setters
}

@GET
@Path("/json")
@Produces(MediaType.APPLICATION_JSON)
public List<Book> getJsonResponse(JAXBElement<Book> book){
    return new ArrayList<Book>();
}

我的想法是球衣会自动找到JacksonJsonProvider ,一个消息作家类,由杰克逊提供,但没有。

My thought was jersey would automatically find the "JacksonJsonProvider", a message writer class, provided by Jackson but it doesn't.

我的lib文件夹

推荐答案

根据mkyong < a href =http://www.mkyong.com/webservices/jax-rs/json-example-with-jersey-jackson/ =nofollow>球衣+杰克逊教程,你需要添加您的web.xml中的 com.sun.jersey.api.json.POJOMappingFeature 参数

According to mkyong jersey+jackson tutorial, you need to add com.sun.jersey.api.json.POJOMappingFeature param in your web.xml to integrate them

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.mkyong.rest</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>

这篇关于休息JAX-RS异常,找不到MessageBodyWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆