4

I am developing a simple Rest API with Spring Boot 2.1.4 and Gradle 5.0. I am using Lombok v1.18.6 to build my classes but when I call the services I am receiving an empty object

I tried adding the getters and setters methods manually and it fixed the problem but I would like to know why lombok is not working in my project.

Also, my IDE is identifying properly the lombok pluging. I am using IntelliJ IDEA

My gradle dependency:

    compileOnly 'org.projectlombok:lombok:1.18.6'
    annotationProcessor 'org.projectlombok:lombok:1.18.6'

My model class:

@Entity
@Data
public class Category implements Serializable {

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

    @NotBlank
    private String name;

    @OneToMany(mappedBy = "category", cascade = CascadeType.ALL, orphanRemoval = true)
    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    private List<Language> languages;
}

My RestController:

@RestController
@RequestMapping("/categories")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping
    public ResponseEntity<List<Category>> findAllCategories(){
        List<Category> categories = categoryService.findAll();
        return new ResponseEntity<List<Category>>(categories, HttpStatus.OK);
    }
}

I am receiving this response:

[
    {
        "languages": []
    }
]

But what I am expecting to receive is:

[
    {
        "idCategory": 1,
        "name": "Backend" 
        "languages": []
    }
]

Actually, I find weird that the only attribute that is being shown is languages, that has the @JsonProperty annotation, why is this happening?

UPDATE

I just realized that my IDE (IntelliJ) is recognizing the lombok pluging and I also have annotation processing enabled but when I try to excecute my code using a getter or an setter, it throws an error:

Error:(18, 26) java: cannot find symbol
  symbol:   method setName(java.lang.String)
  location: class com.ramonparis.cvmanager.model.Category
Ramon Paris
  • 127
  • 2
  • 11
  • Just an advice : you should have separate model for your persistance layer and api layer, so those layer could be independent. – Michał Krzywański Apr 20 '19 at 04:56
  • @michalk Thanks a lot for this advice!! I am still junior as software engineer :P – Ramon Paris Apr 20 '19 at 05:04
  • I disagree with michalk, it depends on the project. If you can keep your code simple, do it. If one day you feel the need to separate the API layer, you will know. – Guillaume F. Apr 20 '19 at 05:21
  • It maybe worthwhile to look into what code lombok generated for that class. I think the intellij lombok plugin has that option. – rdas Apr 20 '19 at 05:27
  • 1
    https://stackoverflow.com/questions/44872427/should-i-use-jpa-entity-in-rest-request-and-or-response – Ryuzaki L Apr 20 '19 at 05:30
  • Could be useful: https://github.com/rzwitserloot/lombok/issues/1563 – Strelok Apr 20 '19 at 14:22

5 Answers5

2

The reason it may not be working for you is if your project is not set to delegate IDE builds to Gradle and annotation processing is not enabled for the project, or is somehow misconfigured.
Settings -> Build, Execution, Deployment -> Build Tools - Gradle -> Runner
Settings -> Build, Execution, Deployment -> Compiler -> Annotation Processors

Have you tried building and running with Gradle from the command line, to rule out misconfiguration in IntelliJ?

Tore Brandtzæg
  • 586
  • 3
  • 5
  • I have both configuration done. Also, I build the .jar from the CLI and run it without any IDE but is giving me the same problem. – Ramon Paris Apr 20 '19 at 20:57
  • Updating! I tried recompiling the project and restarting it with this clean configuration and now is working! After all, this helped me! Thanks a lot!! – Ramon Paris Apr 20 '19 at 21:39
1

Make sure enable the option annotation processing, re run your project that is all make sure enable the option annotation processing

Wilmer Villca
  • 552
  • 4
  • 5
0

I suspect that Hibernate doesn't like the full equals that Lombok generates automatically. You should try:

@Entity
@Data
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
public class Category implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @EqualsAndHashCode.Include
    @Column(name = "id_category")
    private int idCategory;

Hibernate expects its context-cache to match on PrimaryKeys, but Lombok generates an Equals which matches all fields, which means that a single change in the entity will corrupt the cache and maybe prevent the Entity to be loaded correctly.

Guillaume F.
  • 5,905
  • 2
  • 31
  • 59
0

This may be due to result from categoryService.findAll(); is having different mapping names.

e.g. For idCategory, result has id_category. Due to this id_category is not getting mapped to idCategory.

So you can use,

@JsonProperty("idCategory")
Vikas
  • 6,868
  • 4
  • 27
  • 41
  • Thanks! I put this above my attributes and now is returning the expected value. But I just realized that the getters and setters are recognized by IntelliJ but not are working when I excecute my code. So, I am still having problems with Lombok library! Also, I would like to avoid writing the `@JsonProperty` annotation in every attribute of all my model classes – Ramon Paris Apr 20 '19 at 05:45
  • If you don't want `@JsonProperty`, you need to return result with matching attribute names of model class – Vikas Apr 20 '19 at 05:57
0

you can use this annotation

@JsonProperty("idCategory")

the @JsonProperty annotation to indicate the property name in JSON.

Dada
  • 6,313
  • 7
  • 24
  • 43