What is the preferred way to create class that is
Another alternative that is much less verbose:
@Data
@Setter(AccessLevel.NONE)
public class Clazz {
private String field;
}
Of course, you could still have some private method that modifies the field directly, but it would be very unlikely to even have any actual code in a @Data POJO, so this would hopefully not happen.
Disclaimer: This will have the side-effect (perhaps beneficial) of not letting regular Java code create the object since there is only a default constructor with no mutators. To allow for normal construction you will need 2 more annotations:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Setter(AccessLevel.NONE)
public class Clazz {
private String field;
}
Answer by Thomas Fritsch worked perfectly with Spring Boot after adding Jackson Dataformat dependency in pom.
@Data
@Builder(builderClassName = "PointBuilder")
@JsonDeserialize(builder = Point.PointBuilder.class)
public class Point {
private final int x;
private final int y;
@JsonPOJOBuilder(withPrefix = "")
public static class PointBuilder {
// Lombok will add constructor, setters, build method
}
}
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
add ConstructorProperties:
lombok.config
file in an appropriate location with the line:
lombok.anyConstructor.addConstructorProperties = true
@Value
annotation to your class to make it immutableThen serialization and deserialization by Jackson works as expected.
This method:
Edit: 2020-08-16
@Builder
with @Value
causes this solution to fail. (Thanks to comment from @guilherme-blanco below.)
However, if you also add e.g. @AllArgsConstructor
it does still work as expected.I've just solved it using this way:
@Value
@Builder(setterPrefix = "with")
@JsonDeserialize(builder = Clazz.ClazzBuilder.class)
public class Clazz {
private String field;
}
In that case, you have to use the builder methods like withField(...)
, which is the default behaviour used by jackson.
As of 15 October 2020 Lombok (v1.18.16), you should simply be able to use the @Jacksonized annotation.
@Jacksonized @Builder @JsonIgnoreProperties(ignoreUnknown = true) public class JacksonExample { private List<Foo> foos; }
As described in the linked documentation, this annotation:
@JsonIgnoreProperties
), andbuilder().withField(field)
vs builder.field(field)
to the one configured in Lombok.By referencing answer by Joseph K. Strauss I came up with following solution.
Plain lombok annotations that worked out for me looks like this. Following annotations give you immutable classes with builder that can be serialized and deserialized by Jackson.
@Data
@Setter(AccessLevel.NONE)
@Builder(toBuilder = true)
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Clazz {
private String field;
}
I prefer this solution because it requires no additional Jackson specific annotations and no additional lombok specific files