我将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 !
}
}
答案 0 :(得分:2)
要解决此问题,您需要:
1-Lombok插件安装在Intellij IDEA中。 https://projectlombok.org/setup/intellij
添加Lombok IntelliJ插件以添加对IntelliJ的lombok支持:
转到“文件”>“设置”>“插件” 单击浏览存储库... 搜索Lombok插件 点击安装插件 重新启动IntelliJ IDEA
2-为您的项目打开了注释处理。 请参阅-https://stackoverflow.com/a/27430992