Java - Immutable POJOs with Project Lombok and Jackson

I found myself in need of Java value classes that supported Jackson deserialization. Since Jackson gained support for builder objects in version 2.0, it’s now possible to use Project Lombok to generate value classes that support Jackson deserialization.

package todoer.api;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import lombok.Builder;
import lombok.Value;

@JsonDeserialize(builder = TodoEntry.TodoEntryBuilder.class)
@Builder
@Value
public class TodoEntry {
    private final Integer id;

    private final String title;

    private final boolean completed;

    private final int ordering;

    private final String url;

    @JsonPOJOBuilder(withPrefix = "")
    public static final class TodoEntryBuilder {
    }
}

The credit goes Pierre Carrier, who posted the fix on the Project Lombok mailing list.

One limitation is that I don’t think you can rename the fields with @JsonProperty without writing out the builder code and adding the annotations yourself.

Published on by .