Ignoring Unrecognized JSON Fields With Spring & Jackson

Published on March 13, 2015 by

If you try to bind JSON to an object in Spring MVC with Jackson, then you might have seen the below error before.

Could not read JSON: Unrecognized field \”something\” (class com.codingexplained.user.dto.account), not marked as ignorable […]

This error occurs whenever an unknown field is encountered in the JSON; that is, a field specified in the JSON is not available within the binding object. This may be undesirable, and one may just want to ignore if additional fields are provided, as long as the required fields are available. With Jackson, this can very easily be changed with a @JsonIgnoreProperties annotation, as in the below example.


@JsonIgnoreProperties(ignoreUnknown = true)
public class AccountDto {
	...
}

Whenever JSON is mapped to an object of type AccountDto, an exception is no longer thrown if one or more JSON fields do not map to a field in the class. This could for instance be the case within a Spring MVC controller action as in the below example.


@Controller
public class AccountController {
	@RequestMapping(value = "/account/add", method = RequestType.POST)
	public String addAccount(@Valid @RequestBody AccountDto dto) {
		return "add-account";
	}
}

The same can also be accomplished programatically.


// Jackson version 1.9 or earlier
objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);

// Jackson 2.0 or later
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

That’s it! Thank you for reading.

Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

4 comments on »Ignoring Unrecognized JSON Fields With Spring & Jackson«

  1. Anouar

    I’d Like to thank you so much for your helpful post.
    I’d like also to ask you to how to do this please.
    I’m implementing two GET rest methods for returing json.
    getPlayer(), which is returning a player: name, id, age, position.
    getPlayers(), which is returning a list of players, but with this list of players, I do not want to return position and age.
    I mean, how can I add a field for one response and ignore it for another response.
    Thanks

    • Hello Anouar,

      Thank you for your kind words!

      Usually you would be able to use the @JsonIgnore annotation to ignore fields, but this will not work in your case, because you only want to ignore it in certain contexts. Instead, I recommend that you use Data Transfer Objects (DTO) for each scenario. So for your getPlayers() method, you would have a list of a DTO that simply does not include the position and age fields. If you are not familiar with DTOs, then I recommend that you search a bit around for it and hopefully it will help you.

  2. Gilson Silva

    Top! ;)

  3. Hector

    Good article. I try to consume a web service with php but I am getting this error.
    How could I solve this with PHP?

Leave a Reply

Your e-mail address will not be published.