Manish Patel
Manish Patel

Reputation: 4491

Error parsing json with unexpected properties - how to relax parsing rules?

I have an API hosted/written in DropWizard. It consumes JSON and works well

@POST
public void somePost(SomeObject object)

Where SomeObject is a pojo. Jackson deserialises JSON into the object without a problem.

To future proof the "calling code" that uses this API I started to add some properties that are not yet in SomeObject. However (presumably Jackson) now throws an "error parsing json" error.

I would like to relax the rules a bit so that Jackson is a bit more tolerant about fields that it doesn't recognise in the incoming JSON (GSON does that by default). I tried @JsonIgnoreProperties class level annotation but no luck.

Thanks in advance.

Upvotes: 2

Views: 294

Answers (2)

StaxMan
StaxMan

Reputation: 116572

Another option is to just globally allow any and all unknown properties by disabling DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES. It may be dangerous in just ignoring everything that does not map, but for "Open Content" style usage it works great.

Upvotes: 0

James Cube
James Cube

Reputation: 421

You said that you tried @JsonIgnoreProperties. Have you tried it like this?

@JsonIgnoreProperties(ignoreUnknown = true)

Tested above one and it should work.

Upvotes: 4

Related Questions