Spring REST Controller返回带有空数据的JSON [英] Spring REST Controller returns JSON with empty data

查看:1037
本文介绍了Spring REST Controller返回带有空数据的JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Spring Boot Web应用程序.我正在尝试从服务器接收一些数据. Controller返回一个集合,但是浏览器接收到空的JSON-大括号的数量等于来自服务器的对象的数量,但是其内容为空.

I have a simple Spring Boot web application. I'm trying to receive some data from server. The Controller returns a collection, but the browser receives empty JSON - the number of curly brackets is equals to the number of objects from server, but its content is empty.

@RestController
public class EmployeeController {

@Autowired
private EmployeeManagerImpl employeeManagerImpl;

    @RequestMapping(path="/employees", method = RequestMethod.GET)
    public Iterable<Employee> getAllEmployees() {
        Iterable<Employee> employeesIterable = employeeManagerImpl.getAllEmployees();
        return employeesIterable;
    }
}

该方法启动,浏览器显示:

The method fires and a browser shows:

控制台中仅此而已.有什么想法吗?

Nothing more in the console. Any ideas?

Employee.java

Employee.java

@Entity
public class Employee implements Serializable{

    private static final long serialVersionUID = -1723798766434132067L;

    @Id
    @Getter @Setter 
    @GeneratedValue
    private Long id;

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

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

    @Getter @Setter
    private BigDecimal salary;

    public Employee(){

    }
}

推荐答案

我认为您应该将Lombok用作类级别而不是字段级别.

I think you should use Lombok as class level instead of field level.

@Entity
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor    
public class Employee implements Serializable {}

这可以解决您的问题.

这篇关于Spring REST Controller返回带有空数据的JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆