Je développe RESTEasy Exemple. Dans cet exemple, j'utilise toutes les dernières dépendances et déploie la version Tomcat 8.x. Je peux déployer avec succès l'application, mais lorsque je lance l'URL: http: // localhost: 8080/RESTfulExample/rest/restwebservice/list , les erreurs suivantes se produisent. S'il vous plaît guider ce qui ne va pas ici.
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)
Le code que j'ai développé jusqu'à présent pour référence: Pom.xml
<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
@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>
Maintenant je suis capable de résoudre ce problème. J'ai besoin d'ajouter la dépendance suivante dans pom.xml
:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.16.Final</version>
</dependency>
Et 1) je devrais utiliser @Produces(MediaType.APPLICATION_XML)
sur la signature de la méthode pour obtenir la réponse suivante.
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) Si vous voulez utiliser @Produces(MediaType.TEXT_PLAIN)
alors le code vous donnera la sortie suivante qui ne semble pas utile.
[com.mkyong.rest.Student@4d5fd75e, com.mkyong.rest.Student@7715574d]
Alors utilisez 1) solution.
Essayez d'ajouter une version particulière du sérialiseur
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
l'ajout de ces annotations a résolu mon problème.
Pour être plus clair, pour les débutants. ajouter le
@XmlRootElement(name = "yourClassLowerCased")
au début de votre classe, comme
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 = "<databaseName>") //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;
Pour moi, il s’agissait d’essayer de sérialiser le tableau en XML. Si vous le souhaitez, un film d’emballage comme celui-ci est nécessaire:
@XmlRootElement(name = "movies")
@XmlAccessorType(XmlAccessType.FIELD)
public class Movies
{
@XmlElement(name = "movie")
private List<Movie> movies;
public List<Movie> getMovies() {
return movies;
}
public void setMovies(List<Movie> movies) {
this.movies = movies;
}
}
De cette façon, il peut être sérialisé sous l'objet racine.