IDE 不显示 Lombok 为 Jackson 注释生成的 getter 和 setter class
IDE does not show getters and setters generated by Lombok for a Jackson annotated class
我的 Java 项目使用 Intellij Idea 2019.1.2 社区版。我有一个 Jackson 2.9.6 注释的 POJO,它使用 Lombok 1.18.0 为 pojo 生成 getter 和 setter。我有一些 "client" 代码可以将样本 Json 文本反序列化为 Pojo class。反序列化工作正常,没有任何编译问题,并且 pojo 的 class 文件实际上包含所有的 getter 和 setter。但是,IDE 没有显示 "client" 代码中 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 !
}
}
要解决此问题,您需要:
1 - Lombok 插件安装在 Intellij IDEA 中。
https://projectlombok.org/setup/intellij
添加 Lombok IntelliJ 插件以添加对 IntelliJ 的 lombok 支持:
转到文件 > 设置 > 插件
单击浏览存储库...
搜索 Lombok 插件
点击安装插件
重启 IntelliJ IDEA
2 - 为您的项目打开注释处理。
参考 -
我的 Java 项目使用 Intellij Idea 2019.1.2 社区版。我有一个 Jackson 2.9.6 注释的 POJO,它使用 Lombok 1.18.0 为 pojo 生成 getter 和 setter。我有一些 "client" 代码可以将样本 Json 文本反序列化为 Pojo class。反序列化工作正常,没有任何编译问题,并且 pojo 的 class 文件实际上包含所有的 getter 和 setter。但是,IDE 没有显示 "client" 代码中 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 !
}
}
要解决此问题,您需要:
1 - Lombok 插件安装在 Intellij IDEA 中。 https://projectlombok.org/setup/intellij
添加 Lombok IntelliJ 插件以添加对 IntelliJ 的 lombok 支持:
转到文件 > 设置 > 插件 单击浏览存储库... 搜索 Lombok 插件 点击安装插件 重启 IntelliJ IDEA
2 - 为您的项目打开注释处理。 参考 -