Could not find MessageBodyWriter for response object of type: java.util.ArrayList of media type: text/html - in Resteasy

前端 未结 7 1961
傲寒
傲寒 2021-02-18 15:39

I am developing RESTEasy Example. In this example I am using all latest dependencies and deploying om tomcat 8.x version. I can successfully deploy the applicat

相关标签:
7条回答
  • 2021-02-18 16:18

    To be more clear, for beginners. add the

    @XmlRootElement(name = "yourClassLowerCased") at the beginning of your class, like

    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;
    
    0 讨论(0)
  • 2021-02-18 16:22

    Now I am able to solve this issue. I need to add following dependency in pom.xml:

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

    And 1) I should be using @Produces(MediaType.APPLICATION_XML) on method signature to get following response.

    <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) If you want to use @Produces(MediaType.TEXT_PLAIN) then code will gives you following output which doesn't looks useful.

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

    So use 1) solution.

    0 讨论(0)
  • 2021-02-18 16:25

    For me it was about trying to serialize the array to XML. If you would like that it would need a wrapper class like this for movies:

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

    This way it can be serialized under the root object.

    0 讨论(0)
  • 2021-02-18 16:29

    Try to add particular version of serializer

        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>resteasy-jackson-provider</artifactId>
            <version>${resteasy.version}</version>
        </dependency>
    
    0 讨论(0)
  • 2021-02-18 16:29
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    

    adding those annotations solved my issue.

    0 讨论(0)
  • 2021-02-18 16:35

    (For those who find the question, working with quarkus)

    Using this setup routine:

    mvn io.quarkus:quarkus-maven-plugin:0.16.1:create     -DprojectGroupId=com.sample     -DprojectArtifactId=hello-quarkus     -DclassName="com.sample.DemoEndpoint"     -Dpath="/persons"
    

    To fix the issue described here, I had to add this dependency.

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-jsonb</artifactId>
    </dependency>
    

    here is my full pom.xml after I added the above dependency. Again, 99% of the below pom.xml came from the mvn command above.

    <?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>
    
    
        <!-- ADDED MAGIC HERE -->
        <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>
    
    0 讨论(0)
提交回复
热议问题