Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestTemplate ignores @JsonIgnoreProperties(ignoreUnknown = true) annotation

RestTemplate's getForObject method ignores the @JsonIgnoreProperties(ignoreUnknown = true) annotation on the class.

E.g. for

@JsonIgnoreProperties(ignoreUnknown = true)
public class Stock extends AbstractSObject
{
  // fields
}

and

RestTemplate rest = new RestTemplate();
Stock s = rest.getForObject("address", Stock.class);

I'm getting the following error

Could not read JSON: Unrecognized field "SomeField" (class sandbox.Stock), not marked as ignorable

like image 727
ipavlic Avatar asked Apr 03 '14 10:04

ipavlic


1 Answers

Spring's RestTemplate expects com.fasterxml.jackson.annotation.JsonIgnoreProperties.

If you are using the org.codehaus.jackson.annotate.JsonIgnoreProperties annotation from the older package for other purposes, you can manually setup the RestTemplate to ignore unknown properties:

RestTemplate rest = new RestTemplate();

ObjectMapper lax = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

MappingJacksonHttpMessageConverter c = new MappingJacksonHttpMessageConverter();
c.setObjectMapper(lax);

List<HttpMessageConverter<?>> list = new ArrayList<>();
list.add(c);

rest.setMessageConverters(list);
like image 113
ipavlic Avatar answered Oct 25 '22 02:10

ipavlic