No converter found for return value of type: class

2019-09-20 13:59发布

问题:

I m following a spring boot example from a tutorial wich implement a Restful service to get the list of persons from Database table , but I m getting an error when I call : http://localhost:8080/person on IE : No converter found for return value of type: class java.util.ArrayList . So far I tried to add Jackson dependencies from a suggestion on stackoverflow , following my classes, any help is appreciated . SpringbootApplication :

package ben;


@SpringBootApplication
@EnableAutoConfiguration
public class SpringBootWebApplication
{
public static void main (String  [] args) {
    SpringApplication.run(SpringBootWebApplication.class, args);
  }

}

BDE4 class:

package ben.models;

@Entity
@Table(name="BDE4")
public class Bde
{
  @Id
  @Column
  private int id;

  @Column
  private int numero;

  public int getId()
  {
    return id;
  }
  public void setId(int id)
  {
    this.id = id;
  }
  public int getNumero()
  {
    return numero;
  }
  public void setNumero(int numero)
  {
    this.numero = numero;
  }
}

PrsonRepository interface:

package ben.repository;

@Repository
public interface PersonRepository extends CrudRepository<Bde, Integer> {

}

PersonService interface :

package ben.services;
public interface PersonService
{
  public List <Bde> findAll();
}

PersonServiceImpl Class:

package ben.services;

@Service

public class PersonServiceImpl implements PersonService
{
  @Autowired
  private PersonRepository personRepository;

  @Override
  public List<Bde> findAll()
  {
    System.out.println("PersonServiceImpl");
    return (List<Bde>) personRepository.findAll();
  }
}

PersonRest class:

package ben.web;
@RestController
public class PersonRest
{
  @Autowired
  private PersonService personService;

  @RequestMapping("/person")
  public  List <Bde> findAll() {
    List <Bde> persons=(List<Bde>) personService.findAll();
    for (Bde pers:persons)
    {
      System.out.println("id"+pers.getId() +" numero"+pers.getNumero());
    }
    return persons;
  }
}

POM.XML :dependencies

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>BenId</groupId>
  <artifactId>SpringBoot</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>


  <properties>
    <start-class>ben.SpringBootWebApplication</start-class>
    <java.version>1.8</java.version>
    <oracle.driver.version>12.1.0.2</oracle.driver.version>
  </properties>

  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
<!--       <version>2.8.4.RELEASE</version> -->
      <version>2.1.0.RELEASE</version>

  </parent>


  <dependencies>
<!--      <dependency> -->
<!--     <groupId>org.springframework.boot</groupId>  -->
<!--     <artifactId>spring-boot-starter-thymeleaf</artifactId> -->

<!-- </dependency> -->

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>


    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

    <dependency>
      <groupId>org.apache.tomcat.embed</groupId>
      <artifactId>tomcat-embed-jasper</artifactId>
      <version>7.0.52</version>
<!--       <scope>provided</scope> -->
<!--       <version></version> -->
    </dependency>

    <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>4.6.1</version>
            <scope>provided</scope>
        </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <dependency>
            <groupId>com.oracle.jdbc</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>${oracle.driver.version}</version>

     </dependency>

  <!--  <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.7</version>
    </dependency>

    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.7</version>
    </dependency> -->


  </dependencies>

  <build>
       <plugins>
          <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
             <configuration>
                    <executable>true</executable>
                    <mainClass>ben.SpringBootWebApplication</mainClass>
                </configuration>
          </plugin>
       </plugins>
    </build>

</project>

application properties:

spring.mvc.view.prefix:/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
spring.datasource.url=jdbc:oracle:thin:@12.53.2.202:1521:xe
spring.datasource.username=hr
spring.datasource.password=hr
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

回答1:

I just set up your project and its working perfectly fine, I have just changed pom.xml and application.properties. Moreover I am using MySQL as database. Here is pom.xml and properties file. And, also didn't change a single word in other files.

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=yourpassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.generate-ddl=true
spring.jpa.show-sql=true
server.port = 8080

And here is the screenshot of output



回答2:

Try it:

   @GetMapping("/person")
  public  List <Bde> findAll() {
    List <Bde> persons=personService.findAll();
    for (Bde pers:persons)
    {
      System.out.println("id"+pers.getId() +" numero"+pers.getNumero());
    }
    return persons;
  }

And in : class Bde

 @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private int id;