找不到类型的响应对象的MessageBodyWriter:java.util.Array媒体类型列表:text / html – in Resteasy

我正在开发RESTEasy示例。 在这个例子中,我使用所有最新的依赖项并部署om tomcat 8.x版本。 我可以成功部署应用程序,但是当我启动url时: http:// localhost:8080 / RESTfulExample / rest / restwebservice / list ,我看到以下错误即将发生。 请指导这里出了什么问题。

org.jboss.resteasy.core.NoMessageBodyWriterFoundFailure: Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: text/html at org.jboss.resteasy.core.ServerResponseWriter.writeNomapResponse(ServerResponseWriter.java:66) at org.jboss.resteasy.core.SynchronousDispatcher.writeResponse(SynchronousDispatcher.java:466) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:415) at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:202) at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221) at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) 

到目前为止我开发的代码供参考:pom.xml

   JBoss repository https://repository.jboss.org/nexus/content/groups/public-jboss/    1.8 3.0.16.Final 4.12    org.jboss.resteasy resteasy-jaxrs ${resteasy-jaxrs-version}   org.jboss.resteasy resteasy-servlet-initializer ${resteasy-jaxrs-version}   com.sun.xml.bind jaxb-impl 2.2.11   junit junit ${junit.version} test     RESTfulExample   maven-compiler-plugin  ${java.version} ${java.version}     

Student.java

 @XmlRootElement public class Student { private int student_id; private String student_name; private String student_rollnumber; // setters and getters } 

RESTEasyService.java

 @ApplicationPath("/rest") public class RESTEasyService extends Application{ } 

RESTWebServiceJavaExample.java

 @Path("/restwebservice") public class RESTWebServiceJavaExample { private TreeMap webserviceMap= new TreeMap(); public RESTWebServiceJavaExample(){ Student student = new Student(); student.setStudent_name("Ricky"); student.setStudent_rollnumber("AOHP451"); addStudent(student); student = new Student(); student.setStudent_name("Mayer"); student.setStudent_rollnumber("DKLP987"); addStudent(student); } @GET @Path("list") public List getStudents() { List students = new ArrayList(); students.addAll(webserviceMap.values()); return students; } @POST @Path("add") @Produces("text/plain") @Consumes("application/xml") public void addStudent(Student student_param) { int id = webserviceMap.size(); student_param.setStudent_id(id); webserviceMap.put(id, student_param); } } 

web.xml中:

  Restful Web Application  

现在我能够解决这个问题。 我需要在pom.xml添加以下依赖项:

  org.jboss.resteasy resteasy-jaxb-provider 3.0.16.Final  

1)我应该在方法签名上使用@Produces(MediaType.APPLICATION_XML)来获得以下响应。

 This XML file does not appear to have any style information associated with it. The document tree is shown below.   0 Ricky AOHP451   1 Mayer DKLP987   

2)如果你想使用@Produces(MediaType.TEXT_PLAIN)那么代码将为你提供以下看起来@Produces(MediaType.TEXT_PLAIN)输出。

 [com.mkyong.rest.Student@4d5fd75e, com.mkyong.rest.Student@7715574d] 

所以使用1)解决方案。

尝试添加特定版本的序列化程序

   org.jboss.resteasy resteasy-jackson-provider ${resteasy.version}  

更清楚的是,适合初学者。 添加

在类的开头有@XmlRootElement(name = "yourClassLowerCased") ,比如

 package org.dlss.entities; import javax.persistence.*; import javax.xml.bind.annotation.XmlRootElement; @Entity //The class will be a javax.persistence Entity (could be stored in a DB) @Table(name = "person", schema = "public", catalog = "") //Table name @XmlRootElement(name = "person") public class PersonEntity { @Id //Following field will be the id of the table @GeneratedValue(strategy = GenerationType.IDENTITY) //Will be autoincremented when generated for type SERIAL into postgresql private Integer id; 
 @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) 

添加这些注释解决了我的问题。