简体   繁体   English

Spring 引导 Rest API 返回空 Z0ECD11C1D7A287401D148A2kBBD7A2 已使用

[英]Spring Boot Rest API Returns Empty JSON Used with Lombok

I have Spring Boot Application with Web, JPA, H2, Web, Lombok dependency.我有 Spring 启动应用程序与 Web、JPA、H2、ZC6E190B2846433C48EZ39E5C6Z 依赖项。 I have entities as follows我有以下实体

@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Getter
public class Book {

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

    @Column(nullable = false)
    private String name;

    @Column(nullable = false)
    private Integer pageCount;

    @ManyToOne
    @JoinColumn(name = "author_id")
    private Author author;

}
@NoArgsConstructor
@AllArgsConstructor
@Data
@Entity
@Getter
public class Author {

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

    @Column(nullable = false)
    private String firstName;

    @Column
    private String lastName;

}

I am getting following JSON in repose for the /books REST endpoint我正在关注 /books REST 端点的 JSON

[
    {},
    {}
]

If I add getters and setters to entity it works fine.如果我将 getter 和 setter 添加到实体,它工作正常。 How can I get the actual values in response without adding getters setters as I am using @Data annotation当我使用@Data注释时,如何在不添加getter setter的情况下获得实际值作为响应

First of all, In spring it is not a good approach to return the entity object directly from a controller to be converted to json.首先,在 spring 中,直接从 controller 返回实体 object 以转换为 ZDFCDA4D6466DEEC7FECDF25 不是一个好方法。 Use the DTO approach so that you will always have a DTO object returned from the controller endpoint.使用 DTO 方法,以便您始终拥有从 controller 端点返回的 DTO object。 This way you will have more control over the type and structure of data you want to retun from the endpoint.这样,您将可以更好地控制要从端点返回的数据的类型和结构。

Read here about the advantages of using a DTO.阅读此处了解使用 DTO 的优势。

Secondly, verify that your tables follow the basic spring jpa naming conventions or is same as the entity class names if not please add the @Table(name="") annotation to specify the table name.其次,验证您的表是否遵循基本的spring jpa命名约定或与实体 class 名称相同,如果没有,请添加@Table(name="")注释。 Check that data is being populated to your entity classes.检查数据是否填充到您的实体类中。

Remove the @Data annotation.删除@Data注释。

@Data should not be used on JPA Entities as Entity toString , equals , and hashCode methods need to be authored in a very specific manner. @Data不应在 JPA 实体上使用,因为实体toStringequalshashCode方法需要以非常特定的方式编写。 See here for details.有关详细信息,请参见此处

After searching a lot and looking on the answers to my question found a solution.经过大量搜索并查看了我的问题的答案后,找到了解决方案。 If I run my spring boot app using java -jar from cmd it works fine.如果我使用 cmd 中的java -jar运行我的 spring 启动应用程序,它工作正常。 But not with IDE so issue was with IDE configuration.但不是 IDE 所以问题是 IDE 配置。

I am using Eclipse IDE我正在使用 Eclipse IDE

Right Click on your project -> Properties右键单击您的项目->属性

Then from Properties window然后从属性 window

Java Compiler -> Annotation Processing -> Enable Annotation Processing Java 编译器->注解处理->启用注解处理

This option should be checked then annotations gets processed.应选中此选项,然后处理注释。 I followed https://www.baeldung.com/lombok-ide from Tinku's answer on this question.我从 Tinku 对这个问题的回答中关注了https://www.baeldung.com/lombok-ide

在此处输入图像描述

need to add Lombok plugin in maven or gradle file link How to configure Lombok with maven-compiler-plugin?需要在 maven 或 gradle 文件链接中添加 Lombok 插件如何使用 maven-compiler-plugin 配置 Lombok? . . If you are using IDE then need to install Lombok plugin link https://www.baeldung.com/lombok-ide如果您使用的是 IDE 则需要安装 Lombok 插件链接https://www.baeldung.com/lombok-ide

If you are using Eclipse check to have Lombok installed:如果您使用的是 Eclipse,请检查是否安装了 Lombok:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM