简体   繁体   English

Spring Boot - 使用 Lombok 时,Rest Controller 返回空对象

[英]Spring Boot - Rest Controller is returning empty object when using Lombok

I am developing a simple Rest API with Spring Boot 2.1.4 and Gradle 5.0 .我正在使用 Spring Boot 2.1.4 和Gradle 5.0开发一个简单的 Rest API。 I am using Lombok v1.18.6 to build my classes but when I call the services I am receiving an empty object我正在使用Lombok v1.18.6来构建我的类,但是当我调用服务时,我收到了一个空对象

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.我尝试手动添加 getter 和 setter 方法并解决了问题,但我想知道为什么lombok在我的项目中不起作用。

Also, my IDE is identifying properly the lombok pluging.此外,我的 IDE 正在正确识别 lombok 插件。 I am using IntelliJ IDEA我正在使用 IntelliJ IDEA

My gradle dependency:我的gradle依赖:

    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?实际上,我觉得奇怪的是,显示的唯一属性是具有@JsonProperty注释的languages ,为什么会发生这种情况?

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:我刚刚意识到我的 IDE (IntelliJ) 正在识别 lombok 插件,并且我也启用了注释处理,但是当我尝试使用 getter 或 setter 执行我的代码时,它会引发错误:

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. 它可能不适合您的原因是,您的项目未设置为将IDE构建委派给Gradle,并且未对项目启用注释处理,或者以某种方式错误配置。
Settings -> Build, Execution, Deployment -> Build Tools - Gradle -> Runner 设置 - >构建,执行,部署 - >构建工具 - 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? 您是否尝试从命令行构建并运行Gradle,以排除IntelliJ中的错误配置?

I suspect that Hibernate doesn't like the full equals that Lombok generates automatically. 我怀疑Hibernate不喜欢Lombok自动生成的完全等号。 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. Hibernate希望它的上下文缓存在PrimaryKeys上匹配,但是Lombok生成一个匹配所有字段的Equals,这意味着实体中的单个更改将破坏缓存并可能阻止实体正确加载。

This may be due to result from categoryService.findAll(); 这可能是由于categoryService.findAll(); is having different mapping names. 具有不同的映射名称。

eg For idCategory , result has id_category . 例如,对于idCategory ,结果有id_category Due to this id_category is not getting mapped to idCategory . 由于这个id_category没有被映射到idCategory

So you can use, 所以你可以使用,

@JsonProperty("idCategory")

Make sure enable the option annotation processing, re run your project that is all 😉 确保启用选项注释处理,重新运行完全😉的项目 确保启用选项注释处理

你可以用这个注释

@JsonProperty("idCategory")

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

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