0

I'm attempting to use a Jackson flag on the objectMapper

objectMapper.enable(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES);

This should cause object deserialization to fail if a constructor argument is not set in the json. i.e. If a field is missing as opposed to being set to null.

But I noticed that it only works if the object I want to deserialize has a constructor like so

public MyObject(@JsonProperty("id") UUID id, @JsonProperty("url") URL url) {
    this.id = id;
    this.url = url;
}

That's a little problematic as I'd hoped to use lombok's @AllArgsConstructor to generate the constructor. But if the constructor is missing the @JsonProperty(..) the FAIL_ON_MISSING_CREATOR_PROPERTIES check does not work. Instead the parameters are passed in as null.

I've come across some solutions here Can't make Jackson and Lombok work together. But so far they're not working for me.

Any suggestions?

--- Update ---

The annotations on my class are

@Data
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
public class MyClass { ... }
Shane Gannon
  • 4,336
  • 5
  • 32
  • 47

1 Answers1

0

The following combination of annotations work fine with Lombok 1.18.0 and Jackson 2.9 (the most recent versions as of July 2018):

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class Foo {
    private UUID id;
    private String url;
}
String json = "{\n" +
              "  \"id\": \"32783be3-5355-41d2-807b-619e3481d220\",\n" +
              "  \"url\": \"http://example.com\"\n" +
              "}";

ObjectMapper mapper = new ObjectMapper();
Foo foo = mapper.readValue(json, Foo.class);
cassiomolin
  • 101,346
  • 24
  • 214
  • 283