1

I have the following REST call with Spring's (5.0.1) RestTemplate and Jackson 2 (fasterxml) converter:

final List<HttpMessageConverter<?>> messageConverters =   restTemplate.getMessageConverters();

MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();

final ObjectMapper objectMapper = converter.getObjectMapper();
objectMapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);

restTemplate.getMessageConverters().add(converter);

What I don't understand is when the response from the server has an unknown JSON property, it simply sets it to null vs. what I had assumed RestTemplate#getForEntity() throwing an exception during data extraction:

ResponseEntity<MyResponse> responseEntity = restTemplate.getForEntity("http//some-url/api", MyResponse.class); 

Mapping object is simply a Serializable and does not have any Jackson annotation:

public class MyResponse implements Serializable {
  private String propertyOne;
  private String propetyTwo;
}

A response JSON looks like:

{ 
   "propertyOne":"one",
   "badName":"two"    
} 

The mapped object contains a value for propertyOne but null for badName in this case.

Is RestTemplate/Jackson not throwing any exception/error normal in these cases?

What if I want to force the call to throw an exception?

NuCradle
  • 665
  • 1
  • 9
  • 31

1 Answers1

2

Use FAIL_ON_UNKNOWN_PROPERTIES

Feature that determines whether encountering of unknown properties (ones that do not map to a property, and there is no "any setter" or handler that can handle it) should result in a failure (by throwing a JsonMappingException) or not. This setting only takes effect after all other handling methods for unknown properties have been tried, and property remains unhandled.

Feature is enabled by default (meaning that a JsonMappingException will be thrown if an unknown property is encountered).

Example :

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,true);
Community
  • 1
  • 1
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • I explicitly enabled that feature on message converter of `RestTemplate` as described above, but no exception is being thrown. – NuCradle Jun 23 '19 at 03:54
  • configure that property using `configure` method @NuCradle – Ryuzaki L Jun 23 '19 at 04:06
  • So it turned out the way `RestTemplate` bean was being created in the application, it would iterate through its `HttpMessageConverter`'s, if there was an existing `MappingJackson2HttpMessageConverter`, it wouldn't add/configure it. And the reason for an existing one is that, `RestTemplate` adds this Jackson2 message converter in its constructor [link](https://github.com/spring-projects/spring-framework/blob/5.0.x/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java#L159). Thanks for your help. – NuCradle Jun 23 '19 at 14:51
  • so you created `RestTemplate` bean with `MappingJackson2HttpMessageConverter` @NuCradle – Ryuzaki L Jun 23 '19 at 15:09
  • 1
    I get a list of `HttpMessageConverter`'s from `RestTemplate`, iterate through them, and if it does *not* contain `MappingJackson2HttpMessageConverter`, I configure its `ObjectMapper` with above features. In case of **String 5** `RestTemplate`, it seems like the constructor already is adding this converter to its list of message converters. So all I need is to get it from the list, and configure its `ObjectMapper`. – NuCradle Jun 23 '19 at 15:17