IDE不会显示Lombok为Jackson注释类生成的getter和setter [英] IDE does not show getters and setters generated by Lombok for a Jackson annotated class

查看:200
本文介绍了IDE不会显示Lombok为Jackson注释类生成的getter和setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Intellij Idea 2019.1.2社区版用于我的Java项目.我有一个用Jackson 2.9.6注释的POJO,它使用Lombok 1.18.0生成pojo的getter和setter.我有一些客户端"代码将样本Json文本反序列化为Pojo类.反序列化工作正常,没有任何编译问题,并且pojo的类文件实际上具有所有的getter和setter方法.但是,IDE不会在客户端"代码中显示pojo的任何getter和setter.

I use Intellij Idea 2019.1.2 community edition for my Java projects. I have a Jackson 2.9.6 annotated POJO which uses Lombok 1.18.0 to generate the getters and setters for the pojo. I have some "client" code to deserialize a sample Json text to the Pojo class. The deserialization works fine, without any compilation issues and the class file for the pojo actually has all the getters and setters. But, the IDE does not show any getters and setters for the pojo in the "client" code.

使IDE的缓存无效并重新启动它并不能解决问题.我如何找出问题的原因并解决?

Invalidating the caches of the IDE and restarting it did not solve the problem. How do I find out the cause for this problem and fix it ?

Json示例:

{
    "id": "1234",
    "number" : 1,
    "items" : [
        {
            "item1" : "blah...more fields."
        },
        {
            "item2" : "blah...more fields."
        }
    ],
    "someBigObject:" : {
        "my_comment" : "i don't really care about validating this object.",
        "fields" : "more fields here",
        "objects" : "more objects here"
    },
    "message" : "hello"
}

上述Json的Pojo,由 http://www.jsonschema2pojo.org/生成:

Pojo for above Json, generated by http://www.jsonschema2pojo.org/ :

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
        "id",
        "number",
        "items",
        "someBigObject:",
        "message"
})
@Data
@NoArgsConstructor
public class Example {
    @JsonProperty("id")
    @NonNull public String id;
    @JsonProperty("number")
    public Long number;
    @JsonProperty("items")
    public List<Item> items = null;
    @JsonProperty("someBigObject:")
    public Object someBigObject;
    @JsonProperty("message")
    public String message;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    @JsonAnyGetter
    public Map<String, Object> getAdditionalProperties() {
        return this.additionalProperties;
    }

    @JsonAnySetter
    public void setAdditionalProperty(String name, Object value) {
        this.additionalProperties.put(name, value);
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonPropertyOrder({
            "item1",
            "item2"
    })
    @Data
    @NoArgsConstructor
    public static class Item {
        @JsonProperty("item1")
        public String item1;
        @JsonProperty("item2")
        public String item2;
        @JsonIgnore
        private Map<String, Object> additionalProperties = new HashMap<String, Object>();

        @JsonAnyGetter
        public Map<String, Object> getAdditionalProperties() {
            return this.additionalProperties;
        }

        @JsonAnySetter
        public void setAdditionalProperty(String name, Object value) {
            this.additionalProperties.put(name, value);
        }
    }

}

尝试上述pojo的示例代码:

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class JunkTest {
    public static void main(String [] args) throws IOException {
        String json = "Put the json here !!!";
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        Example pojo = mapper.readValue(json, Example.class);
        pojo.getClass();//Cannot see any other getters and setters !
    }

}

推荐答案

要解决此问题,您需要:

To resolve this problem you need :

1-Lombok插件安装在Intellij IDEA中. https://projectlombok.org/setup/intellij

1 - Lombok plugin is installed in Intellij IDEA. https://projectlombok.org/setup/intellij

添加Lombok IntelliJ插件以添加对IntelliJ的lombok支持:

Add the Lombok IntelliJ plugin to add lombok support for IntelliJ:

转到文件">设置">插件" 单击浏览存储库... 搜索Lombok插件 点击安装插件 重新启动IntelliJ IDEA

Go to File > Settings > Plugins Click on Browse repositories... Search for Lombok Plugin Click on Install plugin Restart IntelliJ IDEA

2-为您的项目打开了注释处理. 请参阅- https://stackoverflow.com/a/27430992

2 - Annotation processing is turned on for your project. Refer - https://stackoverflow.com/a/27430992

这篇关于IDE不会显示Lombok为Jackson注释类生成的getter和setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆