I was trying to use Jackson to write a class value to JSON that has Optional as fields:
public class Test {
Optional field = Optional.of(\"
Similar to @Manikandan's answer but add @JsonProperty
to the private field instead of a getter so you don't expose your work around on the public api.
public class Test {
@JsonProperty("field")
private String field;
@JsonIgnore
public Optional<String> getField() {
return Optional.of(field); // or Optional.ofNullable(field);
}
}