javaserializationjacksonlombok

@Accessors(fluent = true) does not work with Jakson


In Spring boot application with Lombok, I have pojo class AccountDTO

@Data
@Builder
@Accessors(fluent = true)
public class AccountDTO  implements Serializable {
    private String identification;
}

My project compiles fine. However, it throws an exception in its execution

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class AccountDTO and no properties discovered to create BeanSerializer

if I removed the annotation @Accessors(fluent = true), then it will work fine without any problems.

How can i make Lombok @Accessors(fluent = true) and Jackson work together ?


Solution

  • I stumbled on this question recently while encountering the same problem and the solution for me was to add an explicit @JsonProperty annotation on the getter generated by Lombok using: @Getter(onMethod = @__(@JsonProperty)).

    In your case, the class would look like this:

    @Data
    @Builder
    @Accessors(fluent = true)
    @Getter(onMethod = @__(@JsonProperty))
    public class AccountDTO  implements Serializable {
        private String identification;
    }