Spring REST 不返回对象的内容

问题描述 投票:0回答:2

我使用 Spring Boot 和一个非常简单的 RestController。控制器查询 MySQL 数据库,就在从控制器方法返回之前,我确实看到返回了五个员工的列表,每个员工都包含有关该员工的信息。 但是,使用 HTTP-Get 请求查询 Spring Rest 应用程序会返回五个空 JSON 结构的数组: 为什么?

我的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>

    <groupId>com.luv2code.springboot</groupId>
    <artifactId>cruddemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>20-hibernate-basic-cruddemo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </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>

我的员工班级:

@Entity
@Table(name = "employee")
public class Employee {

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

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @Column
    private String email;
}

我的休息控制器:

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.luv2code.spring.boot.cruddemo.dao.EmployeeDAO;
import com.luv2code.spring.boot.cruddemo.entity.Employee;

@RestController
@RequestMapping("/api")
public class EmployeeRestController {

    @Autowired
    private EmployeeDAO employeeDAO;

    @GetMapping("/employees")
    public List<Employee> findAll() {
        List<Employee> employees = employeeDAO.findAll();
        return employees;
    }

}

当我在该行放置断点时

回归员工;

我确实看到了五个像这样的非空 Employee 对象:

但是当使用浏览器的 HTTP Get 请求进行查询时,我确实收到了五个空的 JSON 结构,如下所示:

有什么问题吗?

arrays json spring-boot spring-restcontroller spring-rest
2个回答
2
投票

感谢安迪·威尔金森: 我忘记了 Employee 类的 getter 和 setter。 当我把它们放回去时,一切顺利!

AND:它与 MySQL 数据库一起使用的原因是因为我在 Hibernate 中使用字段反射,但显然在 JACKSON / 中使用 setter 反射来序列化为 JSON!


0
投票

有两种方法可以处理您的错误, 1是添加loombook的getter setter 2是在Entity中将private改为public,祝你好运

© www.soinside.com 2019 - 2024. All rights reserved.