Remo
Remo

Reputation: 604

@JsonNaming not working with lombok builder

I am having below class


@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Test {

    String tTObj;
    String value;

    public String gettTObj() {
        return tTObj;
    }

    public void settTObj(final String tTObj) {
        this.tTObj = tTObj;
    }

    public String getValue() {
        return value;
    }

    public void setValue(final String value) {
        this.value = value;
    }

    public static void main(final String[] args) throws IOException {
        final String json = "{\"TTObj\" : \"hi\", \"Value\": \"hello\"}";

        final ObjectMapper objectMapper = new ObjectMapper();

        final Test test = objectMapper.readValue(json, Test.class);

        System.out.println(test);
    }
}

Above one is working fine and able to deserialize.

But if I use lombok builder for deserialization, it's failing.

Below is class with lombok builder used for deserialization

@JsonDeserialize(builder = Test.Builder.class)
@Value
@Builder(setterPrefix = "with", builderClassName = "Builder", toBuilder = true)
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Test {

    String tTObj;
    String value;

    public static void main(final String[] args) throws IOException {
        final String json = "{\"TTObj\" : \"hi\", \"Value\": \"hello\"}";

        final ObjectMapper objectMapper = new ObjectMapper();

        final Test test = objectMapper.readValue(json, Test.class);

        System.out.println(test);
    }
}

got below error

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "TTObj" (class com.demo.Test$Builder), not marked as ignorable (2 known properties: "value", "ttobj"])
 at [Source: (String)"{"TTObj" : "hi", "Value": "hello"}"; line: 1, column: 13] (through reference chain: com.demo.Test$Builder["TTObj"])

How to deserialize "{"TTObj" : "hi", "Value": "hello"}" this using @JsonNaming with lombok builder?

Upvotes: 1

Views: 4196

Answers (1)

Jan Rieke
Jan Rieke

Reputation: 8052

Jackson annotations on the class have to be also present on the builder class to have an effect on deserialization. Automatic copying of @JsonNaming to the builder class was introduced with Lombok 1.18.14.

However, the io.freefair.lombok Gradle plugin in version 5.1.0 uses Lombok version 1.18.12 as default. So that's one version too old (Lombok uses only even numbers as minor/patch level numbers).

You could use a newer version of the Gradle plugin, or configure it manually to use a newer Lombok version via build.gradle:

lombok {
    version = "1.18.20"
}

PS: With Lombok >= 1.18.14, you may consider using @Jacksonized instead of manually configuring your builder:

@Value
@Jacksonized
@Builder(builderClassName = "Builder", toBuilder = true)
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
public class Test {
    ...
}

You don't need the @JsonDeserialize annotation and the "with" prefix in that case.

Upvotes: 2

Related Questions