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
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?
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.
This may be due to result from categoryService.findAll();
is having different mapping names.
eg For idCategory
, result has id_category
. Due to this id_category
is not getting mapped to idCategory
.
So you can use,
@JsonProperty("idCategory")
你可以用这个注释
@JsonProperty("idCategory")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.