Robert Strauch
Robert Strauch

Reputation: 12896

Jackson deserialization feature which fails if not all properties are present?

Let's say I have this POJO:

@Data
public class MyPojo {
  private boolean isEnabled;
  private int timeout;
}

Deserialization from JSON to the above POJO is done with Jackson 2.9. Now, additionally let's say a client sends an "invalid" request. In this context "invalid" means that one of the POJO properties is null or empty. Here, timeout is missing.

{
  "isEnabled": true
}

So far I could not find any Jackson deserialization feature which would make this case fail. Does Jackson provide something like "fail if not all properties are present"?

Upvotes: 0

Views: 272

Answers (1)

Dmitriy Mishenyov
Dmitriy Mishenyov

Reputation: 319

This one won't work:

@JsonProperty(required = true)

You can use constructor-based annotation - it works as a workaround for this unimplemented Jackson feature:

@JsonCreator
public Foo(@JsonProperty(value = "val", required = true) int val) {
    this.val = val;
}

Upvotes: 1

Related Questions