未找到类型为返回值的转换器:类java.util.ArrayList(Spring Boot)

时间:2018-07-10 07:06:08

标签: java mysql spring-boot arraylist

我是Spring Boot的新手,致力于数据库连接。所有相关的类都在这里,只有具有run方法的主类没有发布。

这给了我ArrayList找不到转换器的错误。

如果我做错了任何事情,请帮助我。

// Customer.java

package springbootfirstapp.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer {

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

private String name;
private String phone;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}
public String getPhone() {
return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}

public Customer(int id, String name, String phone) {
    super();
    this.id = id;
    this.name = name;
    this.phone = phone;
}

public Customer() {
    super();
}
}

// CustomerController.java

package springbootfirstapp.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springbootfirstapp.domain.Customer;
import springbootfirstapp.repo.CustomerRepo;

@RestController
@RequestMapping("/customer")
public class CustomerController {

@Autowired
CustomerRepo rp;

@RequestMapping("/findall")
@ResponseBody
public List<Customer> findall()
{
    return rp.findAll();
}
}

// CustomerRepo.java

package springbootfirstapp.repo;
import org.springframework.data.jpa.repository.JpaRepository;
import springbootfirstapp.domain.Customer;
public interface CustomerRepo extends JpaRepository<Customer, Integer> {
}

// pom.xml

<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>SpringBootFirstApp</groupId>
<artifactId>springbootfirstapp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEndoing>UTF-  8</project.reporting.outputEndoing>
    <java.version>1.8</java.version>
</properties>

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

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

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

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

堆栈跟踪:

enter image description here

4 个答案:

答案 0 :(得分:0)

pom.xml

中添加此依赖项
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.1</version>
</dependency>

答案 1 :(得分:0)

如果我正确理解您的问题,则需要在pom中添加jackson mapper。 您可以尝试一次,看看转换器问题是否解决。

答案 2 :(得分:0)

此类错误的通用准则是检查:

  1. 没有Args构造函数
  2. 字母和二传手
  3. 杰克逊依赖项

由于前两个不是问题,请尝试添加以下两个依赖项,并让我们知道它是否有效。

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

答案 3 :(得分:0)

谢谢大家的帮助... 通过添加Jackson Mapper依赖项可以解决问题

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

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

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.5.2</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.5.2</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.5.2</version>
</dependency>