Updated 2018-11-10: Lombok 1.18.4 released
Updated 2020-10-18: Lombok 1.18.16 released
Lombok 1.18.16 contains the new @Jacksonized annotation. With it, you can simply write:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SubA.class),
@JsonSubTypes.Type(value = AnotherSub.class)
})
@Jacksonized
@SuperBuilder
// more annotations...
public abstract class AbstractA {
@JsonProperty
protected final String superProperty;
}
@Jacksonized
@SuperBuilder
@JsonTypeName("SubA")
// more annotations...
public class SubA extends AbstractA {
@JsonProperty
private final String fieldA;
}
This will automatically insert all necessary Jackson annotations and do some adjustments to the generated builder. No need to customized anymore.
For earlier Lombok versions between 1.18.4 and 1.18.12, this is the way to go:
For Lombok's @Builder and @SuperBuilder to work with Jackson, you have to add the builder class header manually and place a @JsonPOJOBuilder(withPrefix="") on it. Lombok will then generate only the remainder of the builder class. This is necessary because Jackson's default is that the builder's setter methods have "with" as prefix, but Lombok's builders don't have any prefix (and Lombok is not and will probably never be configurable in this regard).
When @SuperBuilder was introduced in Lombok 1.18.2, it was not customizable (i.e., you could not manually add the builder class header). As a result, using @SuperBuilder with Jackson was not easily possible.
This changed with Lombok 1.18.4 (see this pull request): @SuperBuilder is now (at least partially) customizable, and this allows us to add the annotation. Beware that the code generated by @SuperBuilder is quite complex and heavily loaded with generics. To avoid accidentally messing up the code, you should have a look at the delombok output and copy/paste the class header from there. Here, you need add the builder implementation class header and put the annotation on it:
@JsonPOJOBuilder(withPrefix="")
static final class SubABuilderImpl extends SubABuilder<SubA, SubABuilderImpl> {
}
Note that you have to broaden the visibility of SubABuilderImpl to at least package-private.
The @JsonDeserialize annotation must also refer to the builder implementation class, not the abstract builder:
@JsonDeserialize(builder = SubA.SubABuilderImpl.class)
Answer from Jan Rieke on Stack OverflowUpdated 2018-11-10: Lombok 1.18.4 released
Updated 2020-10-18: Lombok 1.18.16 released
Lombok 1.18.16 contains the new @Jacksonized annotation. With it, you can simply write:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SubA.class),
@JsonSubTypes.Type(value = AnotherSub.class)
})
@Jacksonized
@SuperBuilder
// more annotations...
public abstract class AbstractA {
@JsonProperty
protected final String superProperty;
}
@Jacksonized
@SuperBuilder
@JsonTypeName("SubA")
// more annotations...
public class SubA extends AbstractA {
@JsonProperty
private final String fieldA;
}
This will automatically insert all necessary Jackson annotations and do some adjustments to the generated builder. No need to customized anymore.
For earlier Lombok versions between 1.18.4 and 1.18.12, this is the way to go:
For Lombok's @Builder and @SuperBuilder to work with Jackson, you have to add the builder class header manually and place a @JsonPOJOBuilder(withPrefix="") on it. Lombok will then generate only the remainder of the builder class. This is necessary because Jackson's default is that the builder's setter methods have "with" as prefix, but Lombok's builders don't have any prefix (and Lombok is not and will probably never be configurable in this regard).
When @SuperBuilder was introduced in Lombok 1.18.2, it was not customizable (i.e., you could not manually add the builder class header). As a result, using @SuperBuilder with Jackson was not easily possible.
This changed with Lombok 1.18.4 (see this pull request): @SuperBuilder is now (at least partially) customizable, and this allows us to add the annotation. Beware that the code generated by @SuperBuilder is quite complex and heavily loaded with generics. To avoid accidentally messing up the code, you should have a look at the delombok output and copy/paste the class header from there. Here, you need add the builder implementation class header and put the annotation on it:
@JsonPOJOBuilder(withPrefix="")
static final class SubABuilderImpl extends SubABuilder<SubA, SubABuilderImpl> {
}
Note that you have to broaden the visibility of SubABuilderImpl to at least package-private.
The @JsonDeserialize annotation must also refer to the builder implementation class, not the abstract builder:
@JsonDeserialize(builder = SubA.SubABuilderImpl.class)
A working solution in Eclipse, note that the Lombok IntelliJ integration is not supporting all features, therefore the code compiles fine in Eclipse and with javac but IntelliJ thinks it's broken but executes the code without an issue.
public static ObjectMapper createObjectMapper() {
final ObjectMapper mapper = new ObjectMapper();
mapper.setAnnotationIntrospector(new JacksonAnnotationIntrospector() {
@Override
public JsonPOJOBuilder.Value findPOJOBuilderConfig(final AnnotatedClass ac) {
if (ac.hasAnnotation(JsonPOJOBuilder.class)) {
return super.findPOJOBuilderConfig(ac);
}
return new JsonPOJOBuilder.Value("build", "");
}
});
return mapper;
}
public static void main(final String[] args) throws Exception {
final ObjectMapper objectMapper = createObjectMapper();
final String serializedForm = objectMapper.writeValueAsString(SubA.builder().build());
System.out.println(serializedForm);
final SubA anA = objectMapper.readValue(serializedForm, SubA.class);
System.out.println(anA);
}
@Getter
@EqualsAndHashCode(callSuper = true)
@Accessors(fluent = true, chain = true)
@SuperBuilder
@JsonDeserialize(builder = SubA.SubABuilderImpl.class)
@JsonTypeName("SubA")
public static class SubA extends AbstractA {
@JsonProperty
private final String fieldA;
}
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SubA.class)
})
@Getter
@Accessors(fluent = true, chain = true)
@SuperBuilder
public static abstract class AbstractA {
@JsonProperty
protected final String superProperty;
}
AFAIK, @Data generates a @NoArgsConstructor, which is just wrong. Actually, @Data is wrong per se, as it's meant for mutable classes; @Value would be better, but it can't deal with the super constructor either.
So remove @Data, add @Getter, @EqualsAndHashCode, @ToString and whatever you need. Don't forget to add callSuper=true in the subclass.
This is effectively the constructor that Lombok should be constructing and invoking on the SuperBuilder.build() operation.
public Child(
final String userId,
final Instant requestingTime,
final Instant beginningDate,
final Instant endingDate,
final Collection<String> fields) {
super(userId, requestingTime);
this.beginningDate = beginningDate;
this.endingDate = endingDate;
this.fields= fields;
}
No, that's not how SuperBuilder works. This is actually Lombok can't do as it can't see the super fields. Instead, the builder uses something like
public Child(ChildBuilder b) {
super(b);
this.beginningDate = b.beginningDate;
this.endingDate = b.endingDate;
this.fields= b.fields;
}
You can believe what Jan Rieke says, he wrote it.
@Data annotation implicitly generate code for below mentioned functionalities:
- setter
- getter
- toString
- equallAndHashCode
- constructor(for required arguments only)
It means constructor declaration of loombok will generate code for Parent class will be as mentioned below:
Person(String userId, Instant requestingTime)
Similarly for Child class:
Child(Instant beginningDate, Instant endingDate, Collection fields)
Now as your program is throwing exception that
Parent() is undefined in parent class.
Please annotate your class with :
@NoArgsConstructor
This will generate required default constructor.
I recently tried customizing @SuperBuilder using Lombok 1.18.8 and IntelliJ, and it worked fine. The only issue I faced was, I lost ability to use toBuilder flag in SuperBuilder - @SuperBuilder(toBuilder=true).
Below is the code to override @SuperBuilder methods.
public static abstract class ChildBuilder<C extends Child, B extends ChildBuilder<C, B>>
extends ParentBuilder<C, B> {
private LocalDate date;
public B date(String dateStr) {
this.date = LocalDate.parse(dateStr);
return self();
}
}
I added my working code here: Customize SuperBuilder in Lombok
When @SuperBuilder was introduced in 1.18.2, customising it was not possible. If you try, Lombok 1.18.2 gives you the error message SuperBuilder does not support customized builders.
However, Lombok 1.18.4 added limited customisation possibilities of @SuperBuilder. (It's limited because you cannot modify setter methods, but you can add your own methods and modify build() and builder().)
The generated @SuperBuilder code is quite complex and differs from @Builder. To avoid accidently messing up the generics-loaded builder code, you should start by copying the builder class header from the delombok output.
In your case (adding a new setter method), customize the abstract builder class ParentBuilder (and not the ParentBuilderImpl). Have a look at the delomboked code to find out how your setter should be defined, especially the return type.
This is the customized builder code for your example:
public abstract static class ParentBuilder<C extends Parent, B extends ParentBuilder<C, B>> {
public B setAllTo(final int value) {
return a(value).b(value);
}
}
With Lombok 1.18.4, this compiles and works as expected.