2016-03-30 5 views
4

開発中RESTEasy例。この例では、すべての最新の依存関係を使用しており、om tomcat 8.xバージョンを導入しています。私は正常にアプリケーションを展開することができますが、私はURLを起動しているとき:http://localhost:8080/RESTfulExample/rest/restwebservice/list、私は次のエラーが来て参照してください。ここで何がうまくいかないのかご案内してください。 のpom.xmlタイプの応答オブジェクトのMessageBodyWriterが見つかりませんでした:java.util.Arrayメディアタイプのリスト:text/html - Resteasy

<repositories> 
     <repository> 
      <id>JBoss repository</id> 
      <url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url> 
     </repository> 
    </repositories> 

    <properties> 
     <java.version>1.8</java.version> 
     <resteasy-jaxrs-version>3.0.16.Final</resteasy-jaxrs-version> 
     <junit.version>4.12</junit.version> 
    </properties> 

    <dependencies> 

     <dependency> 
      <groupId>org.jboss.resteasy</groupId> 
      <artifactId>resteasy-jaxrs</artifactId> 
      <version>${resteasy-jaxrs-version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.jboss.resteasy</groupId> 
      <artifactId>resteasy-servlet-initializer</artifactId> 
      <version>${resteasy-jaxrs-version}</version> 
     </dependency> 

     <dependency> 
      <groupId>com.sun.xml.bind</groupId> 
      <artifactId>jaxb-impl</artifactId> 
      <version>2.2.11</version> 
     </dependency> 

     <dependency> 
      <groupId>junit</groupId> 
      <artifactId>junit</artifactId> 
      <version>${junit.version}</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <!-- Project Build --> 
    <build> 
     <finalName>RESTfulExample</finalName> 
     <plugins> 
      <plugin> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>${java.version}</source> 
        <target>${java.version}</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

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:私は参考のためにこれまでに開発されたコード

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) 

@Path("/restwebservice") 
public class RESTWebServiceJavaExample { 
    private TreeMap<Integer, Student> 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<Student> getStudents() { 
     List<Student> students = new ArrayList<Student>(); 
     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:

<web-app id="WebApp_ID" version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
    <display-name>Restful Web Application</display-name> 

</web-app> 

答えて

4

今、私はこの問題を解決することができています。私はpom.xmlに、次の依存関係を追加する必要があります。

<dependency> 
     <groupId>org.jboss.resteasy</groupId> 
     <artifactId>resteasy-jaxb-provider</artifactId> 
     <version>3.0.16.Final</version> 
</dependency> 

そして、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. 
<collection> 
<student> 
<student_id>0</student_id> 
<student_name>Ricky</student_name> 
<student_rollnumber>AOHP451</student_rollnumber> 
</student> 
<student> 
<student_id>1</student_id> 
<student_name>Mayer</student_name> 
<student_rollnumber>DKLP987</student_rollnumber> 
</student> 
</collection> 

2)あなたは@Produces(MediaType.TEXT_PLAIN)を使用したい場合は、コードの意志が便利になりますしない出力、次のあなたに与えます。

[[email protected], [email protected]] 

したがって、1)ソリューションを使用します。

1

は、シリアライザ

<dependency> 
     <groupId>org.jboss.resteasy</groupId> 
     <artifactId>resteasy-jackson-provider</artifactId> 
     <version>${resteasy.version}</version> 
    </dependency> 
の特定のバージョンを追加しよう
関連する問題