Ich entwickle RESTEasy Beispiel. In diesem Beispiel verwende ich alle aktuellen Abhängigkeiten und setze die Tomcat 8.x-Version ein. Ich kann die Anwendung erfolgreich bereitstellen, aber beim Starten der URL: http: // localhost: 8080/RESTfulExample/rest/restwebservice/list sehe ich folgende Fehler. Bitte führen Sie aus, was hier schief läuft.
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)
Der Code, den ich bisher als Referenz entwickelt habe: 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>
Jetzt kann ich dieses Problem lösen. Ich muss folgende Abhängigkeit in pom.xml
hinzufügen:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.16.Final</version>
</dependency>
Und 1) Ich sollte @Produces(MediaType.APPLICATION_XML)
für die Methodensignatur verwenden, um die folgende Antwort zu erhalten.
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) Wenn Sie @Produces(MediaType.TEXT_PLAIN)
verwenden möchten, erhalten Sie folgende Ausgabe, die nicht sinnvoll erscheint.
[[email protected], [email protected]]
Also 1) Lösung verwenden.
Versuchen Sie, eine bestimmte Version des Serializers hinzuzufügen
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jackson-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
(Für diejenigen, die die Frage finden, arbeiten mit Quarkus)
Verwenden dieser Setup-Routine:
mvn io.quarkus:quarkus-maven-plugin:0.16.1:create -DprojectGroupId=com.sample -DprojectArtifactId=hello-quarkus -DclassName="com.sample.DemoEndpoint" -Dpath="/persons"
Um das hier beschriebene Problem zu beheben, musste ich diese Abhängigkeit hinzufügen.
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
hier ist meine vollständige pom.xml, nachdem ich die obige Abhängigkeit hinzugefügt habe. Wieder kamen 99% von if aus dem obigen Befehl mvn.
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.Apache.org/POM/4.0.0 http://maven.Apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.Apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample</groupId>
<artifactId>hello-quarkus</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<surefire-plugin.version>2.22.0</surefire-plugin.version>
<quarkus.version>0.16.1</quarkus.version>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-bom</artifactId>
<version>${quarkus.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jsonb</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.version}</version>
<executions>
<execution>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<configuration>
<systemProperties>
<Java.util.logging.manager>org.jboss.logmanager.LogManager</Java.util.logging.manager>
</systemProperties>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>native</id>
<activation>
<property>
<name>native</name>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-maven-plugin</artifactId>
<version>${quarkus.version}</version>
<executions>
<execution>
<goals>
<goal>native-image</goal>
</goals>
<configuration>
<enableHttpUrlHandler>true</enableHttpUrlHandler>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>${surefire-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemProperties>
<native.image.path>${project.build.directory}/${project.build.finalName}-runner</native.image.path>
</systemProperties>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
durch das Hinzufügen dieser Anmerkungen wurde mein Problem behoben.
Um es klarer zu machen, für Anfänger. Ergänzen Sie die
@XmlRootElement(name = "yourClassLowerCased")
am Anfang Ihres Unterrichts, wie
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;
Für mich ging es darum, das Array in XML zu serialisieren. Wenn Sie möchten, dass eine solche Wrapper-Klasse für Filme benötigt wird:
@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;
}
}
Auf diese Weise kann es unter dem Stammobjekt serialisiert werden.