42

What is the preferred way to create class that is

  • Immutable
  • Can be serialized/deserialized with Jackson
  • Human-readable and with low level of boilerplate

Preferably, I would have liked something like this to work:

@Data(onConstructor = @__(@JsonCreator))

and then have all fields to be private final. However, this does not even compile (and I'm not sure why). Using

@AllArgsConstructor(onConstructor = @__(@JsonCreator))

will compile but only yields

InvalidDefinitionException: No serializer found for class

9 Answers 9

60

add ConstructorProperties:

  • Create a lombok.config file in an appropriate location with the line: lombok.anyConstructor.addConstructorProperties = true
  • Add lombok @Value annotation to your class to make it immutable

Then serialization and deserialization by Jackson works as expected.

This method:

Edit: 2020-08-16

  • Note: Using @Builder with @Value causes this solution to fail. (Thanks to comment from @guilherme-blanco below.) However, if you also add e.g. @AllArgsConstructor it does still work as expected.

Edit: 2021-08-19

  • Note: When you add or change a lombok.config file the change is not picked up unless you do a rebuild (clean then build). I have been caught out by this a few times.
  • @Jacksonized annotation solution is another method to achieve the desired outcome for the specific classes annotated. However, I personally prefer not to need to remember to annotate every class used for deserialization. Using lombok.config removes this overhead.
1
  • 3
    BEWARE: This approach does not work in conjunction with @Builder. May 12, 2020 at 15:21
42

As of 15 October 2020 (Lombok v1.18.16), you should simply be able to use the @Jacksonized annotation.

@Jacksonized @Builder
@JsonIgnoreProperties(ignoreUnknown = true)
public class JacksonExample {
  private List<Foo> foos;
}

As described in the linked documentation, this annotation:

  • configures Jackson to use the builder for deserialisation,
  • copies field-specific configuration down from the annotated class to the generated builder (e.g. @JsonIgnoreProperties), and
  • aligns the Jackson prefix used on builder methods (e.g. builder().withField(field) vs builder.field(field) to the one configured in Lombok.
1
  • 6
    Can we upvote this answer to the first place? It works like a charm :) Jan 29, 2021 at 1:08
29

You can use Lombok's @Builder annotation to generate a builder for your immutable POJO class. But making the Lombok-generated builder usable by Jackson's deserialization is somewhat tricky.

Example:

An immutable POJO class:

@Data
@Builder(builderClassName = "PointBuilder")
@JsonDeserialize(builder = Point.PointBuilder.class)
public class Point {

    private final int x;

    private final int y;

    @JsonPOJOBuilder(withPrefix = "")
    public static class PointBuilder {
        // Lombok will add constructor, setters, build method
    }
}

POJO outline

Here is a JUnit test to verify the serialization/deserialization:

public class PointTest extends Assert {

    private ObjectMapper objectMapper = new ObjectMapper();

    @Test
    public void testSerialize() throws IOException {
        Point point = new Point(10, 20);
        String json = objectMapper.writeValueAsString(point);
        assertEquals("{\"x\":10,\"y\":20}", json);
    }

    @Test
    public void testDeserialize() throws IOException {
        String json = "{\"x\":10,\"y\":20}";
        Point point = objectMapper.readValue(json, Point.class);
        assertEquals(new Point(10, 20), point);
    }
}
1
  • 3
    I've been using this approach for awhile now, and it is very, very effective. One very small improvement is that I always name my builder classes _Builder. That way when I copy paste to create a new class, etc... I'm not having to remember to change the string in the builderClassName. I used to call the classes Builder (without the underscore), but that has weird conflicts with the @Builder annotation class when you do this with inner static classes.
    – Kevin Day
    May 10, 2018 at 1:13
6

By referencing answer by Joseph K. Strauss I came up with following solution.

Plain lombok annotations that worked out for me looks like this. Following annotations give you immutable classes with builder that can be serialized and deserialized by Jackson.

    @Data
    @Setter(AccessLevel.NONE)
    @Builder(toBuilder = true)
    @AllArgsConstructor
    @NoArgsConstructor(access = AccessLevel.PRIVATE)
    public class Clazz {
        private String field;
    } 

I prefer this solution because it requires no additional Jackson specific annotations and no additional lombok specific files

1
  • Thank for this. But seems :
    – Breton F.
    Dec 11, 2020 at 16:34
3

Another alternative that is much less verbose:

@Data
@Setter(AccessLevel.NONE)
public class Clazz {
    private String field;
} 

Of course, you could still have some private method that modifies the field directly, but it would be very unlikely to even have any actual code in a @Data POJO, so this would hopefully not happen.

Disclaimer: This will have the side-effect (perhaps beneficial) of not letting regular Java code create the object since there is only a default constructor with no mutators. To allow for normal construction you will need 2 more annotations:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Setter(AccessLevel.NONE)
public class Clazz {
    private String field;
} 
3

The simplest way to configure immutable class for Jackson is to use lombok annotation: @Value and @Jacksonized:

    @Jacksonized
    @Builder
    @Value
    class Foo {

        
    }
2

Try the following set of annotations for your pojo:

@Value
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
@AllArgsConstructor
2

I've just solved it using this way:

@Value
@Builder(setterPrefix = "with")
@JsonDeserialize(builder = Clazz.ClazzBuilder.class)
public class Clazz {
    private String field;
}

In that case, you have to use the builder methods like withField(...), which is the default behaviour used by jackson.

0

Answer by Thomas Fritsch worked perfectly with Spring Boot after adding Jackson Dataformat dependency in pom.

@Data
@Builder(builderClassName = "PointBuilder")
@JsonDeserialize(builder = Point.PointBuilder.class)
public class Point {

    private final int x;

    private final int y;

    @JsonPOJOBuilder(withPrefix = "")
    public static class PointBuilder {
        // Lombok will add constructor, setters, build method
    }
}

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-xml -->
<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.