I want to deserialize Java Optional field in lombok builder. Below is my code
@JsonDeserialize(builder = AvailabilityResponse.Builder.class)
@Getter
@ToString
@EqualsAndHashCode
@Builder(setterPrefix = "with", builderClassName = "Builder", toBuilder = true)
public class AvailabilityResponse {
private final List<Train> trainDetails;
private final String name;
private final Optional<String> detail;
public static class Builder {
@JacksonXmlProperty(localName = "TRAIN")
private List<Train> trainDetails;
@JacksonXmlProperty(localName = "NAME")
private String name;
@JacksonXmlProperty(localName = "DETAIL_TRAIN", isAttribute = true)
private String detail;
public AvailabilityResponse build() {
return new AvailabilityResponse(
trainDetails, // I have field validation here. if null or empty throwing Exception
name, // I have field validation here. if null or empty throwing Exception
Optional.ofNullable(detail)); // This is Optional field
}
}
}
If I override the builder method like below, able to deserialize
private Optional<String> name; // this is static builder class field
@JacksonXmlProperty(localName = "DETAIL_TRAIN", isAttribute = true)
public Builder detail(final String theDetail) {
this.detail = Optional.ofNullable(theDetail);
return this;
}
I have used setterPrefix ="with" in @Builder. But if I override the above method with "with" prefix, its not working.
Please someone help me to acheive this