【问题标题】:IDE does not show getters and setters generated by Lombok for a Jackson annotated classIDE 不显示 Lombok 为 Jackson 注释类生成的 getter 和 setter
【发布时间】:2019-10-08 06:49:05
【问题描述】:

我将 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。

使 IDE 的缓存无效并重新启动它并没有解决问题。如何找出这个问题的原因并解决它?

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/生成:

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 !
    }

}

【问题讨论】:

标签: java json intellij-idea jackson lombok


【解决方案1】:

要解决此问题,您需要:

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

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

转到文件 > 设置 > 插件 单击浏览存储库... 搜索龙目岛插件 点击安装插件 重启 IntelliJ IDEA

2 - 已为您的项目启用注释处理。 参考-https://stackoverflow.com/a/27430992

【讨论】:

    猜你喜欢
    • 2020-05-19
    • 2012-08-01
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多