0

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.

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 ?

Sample 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"
}

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);
        }
    }

}

Sample code to try the above 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 !
    }

}
MasterJoe
  • 2,103
  • 5
  • 32
  • 58
  • 2
    Did you enable annotation processors in the IntelliJ settings? See also https://stackoverflow.com/questions/17729384/lombok-added-but-getters-and-setters-not-recognized-in-intellij-idea?rq=1 – Mark Rotteveel May 21 '19 at 20:22
  • @MarkRotteveel - Thanks. This gave me the hints to solve my problem. – MasterJoe May 21 '19 at 20:45

1 Answers1

2

To resolve this problem you need :

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

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

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

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

MasterJoe
  • 2,103
  • 5
  • 32
  • 58