简体   繁体   English

其余的JAX-RS异常,找不到MessageBodyWriter

[英]Rest JAX-RS exception, MessageBodyWriter not found

I am using Jersey 2.13 我正在使用Jersey 2.13

I get MessageBoddyWriter not found exception when I try to access a resource via a url in a browser. 当我尝试通过浏览器中的URL访问资源时,出现MessageBoddyWriter找不到异常。

Exception: 例外:

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

I have another method that produces "APPLICATION_XML" and that seems to work fine. 我还有另一种产生“ APPLICATION_XML”的方法,它似乎工作正常。

@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>();
}

My thought was jersey would automatically find the "JacksonJsonProvider" , a message writer class, provided by Jackson but it doesn't. 我的想法是,球衣会自动找到由Jackson提供的消息"JacksonJsonProvider""JacksonJsonProvider" ,但没有。

My lib folder: 我的lib文件夹:

在此处输入图片说明

According to mkyong jersey+jackson tutorial, you need to add com.sun.jersey.api.json.POJOMappingFeature param in your web.xml to integrate them 根据mkyong jersey + jackson教程,您需要在web.xml中添加com.sun.jersey.api.json.POJOMappingFeature参数以将其集成

<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>

Starting with Jersey 2.9, automatic discovery of converter classes has been disabled. 从Jersey 2.9开始,已禁用自动发现转换器类。 You have to register the converter class of your JSON library with Jersey manually. 您必须使用Jersey手动注册JSON库的转换器类。 Here is what I do (I'm using Genson): 这是我的工作(我正在使用Genson):

@ApplicationPath( "/api/library" )
public class RestService extends Application {
   @Override
   public Set<Class<?>> getClasses( ) {
      final Set<Class<?>> returnValue = new HashSet<Class<?>>( );
      returnValue.add( GensonJsonConverter.class );
      return returnValue;
   }
}

If you want to make your application code support both xml and json you need to create a wrapper object to support the collection rather than returning a GenericType. 如果要使应用程序代码同时支持xml和json,则需要创建一个包装对象以支持该集合,而不是返回GenericType。 The PojoMappingFeature will work as it uses the native Jackson ObjectMapper instead of using the jaxb annotations for marshalling the object. PojoMappingFeature将起作用,因为它使用本机Jackson的ObjectMapper而不是使用jaxb注释来编组对象。

If you want it to be portable the best solution is to create a simple wrapper object as such. 如果您希望它具有可移植性,那么最好的解决方案是像这样创建一个简单的包装对象。

@XmlRootElement
public class Books {

   private List<Book> books;

   public Books() {
   }

   public Books(List<Book> books) {
       this.books = books;
   }

   @XmlElement(name="book")
   public List<Book> getBooks() {
      return books;
   }

   public void setBooks(List<Book> books) {
      this.books = books;
   }
}


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

If you decide you want to also support MediaType.APPLICATION_XML then this is required. 如果您决定还支持MediaType.APPLICATION_XML,则这是必需的。 This will also solve the json (jackson) problem. 这也将解决json(jackson)问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM