Search code examples
javaspring-bootrestlombokspring-rest

Spring Boot Rest API Returns Empty JSON Used with Lombok


I have Spring Boot Application with Web, JPA, H2, Web, Lombok dependency. 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

[
    {},
    {}
]

If I add getters and setters to entity it works fine. How can I get the actual values in response without adding getters setters as I am using @Data annotation


Solution

  • 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. But not with IDE so issue was with IDE configuration.

    I am using Eclipse IDE

    Right Click on your project -> Properties

    Then from Properties window

    Java Compiler -> Annotation Processing -> Enable Annotation Processing

    This option should be checked then annotations gets processed. I followed https://www.baeldung.com/lombok-ide from Tinku's answer on this question.

    enter image description here