I have a simple json object.
{"id": 1, "name": "John"}
My class for reading this object is the following.
@Getter
public class Foo {
@Setter
private long id;
@NotNull
private String name;
}
I am using lombok for generating the getters and annotating not-null fields with jetbrain's @NotNull annotation.
String json = "{\"id\": 1, \"name\": \"John\"}";
Foo foo = new Gson().fromJson(json, Foo.class);
foo.setId(42);
Reading the json works just fine.
The only problem that I have that I am getting an error from the IDE Not-null fields must be initialized from my name field.
It is technically correct, my name field is not initalized in the constructor.
However, I am using Gson to deserialize my object and know that name is never null. I do not want to create a normal object, just read from json.
Also I need the annotation @NotNull, because lomboks @Getter will generate me a getter with an @NotNull annotation.
I also have some cases where I want to replace one of my field (id) with another value.
I do not want to use a plain Java getter or remove the annoation from my attribute.
What can I do?