如果 JSON 具有无效的 属性 名称,RestTemplate (w/ Jackson) 调用是否应该失败
Should RestTemplate (w/ Jackson) Call Fail If JSON Has an Invalid Property Name
我使用 Spring 的 (5.0.1) RestTemplate
和 Jackson 2 (fasterxml) 转换器进行了以下 REST 调用:
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);
我不明白的是,当服务器的响应有一个未知的 JSON 属性 时,它只是将其设置为 null
而不是我假设的 RestTemplate#getForEntity()
在数据提取期间抛出异常:
ResponseEntity<MyResponse> responseEntity = restTemplate.getForEntity("http//some-url/api", MyResponse.class);
映射对象只是一个 Serializable
,没有任何 Jackson 注释:
public class MyResponse implements Serializable {
private String propertyOne;
private String propetyTwo;
}
响应 JSON 看起来像:
{
"propertyOne":"one",
"badName":"two"
}
在这种情况下,映射对象包含 propertyOne
的值,但 badName
的值 null
。
在这些情况下,RestTemplate
/Jackson
不抛出任何 exception/error 是否正常?
如果我想强制调用抛出异常怎么办?
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).
示例:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,true);
我使用 Spring 的 (5.0.1) RestTemplate
和 Jackson 2 (fasterxml) 转换器进行了以下 REST 调用:
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);
我不明白的是,当服务器的响应有一个未知的 JSON 属性 时,它只是将其设置为 null
而不是我假设的 RestTemplate#getForEntity()
在数据提取期间抛出异常:
ResponseEntity<MyResponse> responseEntity = restTemplate.getForEntity("http//some-url/api", MyResponse.class);
映射对象只是一个 Serializable
,没有任何 Jackson 注释:
public class MyResponse implements Serializable {
private String propertyOne;
private String propetyTwo;
}
响应 JSON 看起来像:
{
"propertyOne":"one",
"badName":"two"
}
在这种情况下,映射对象包含 propertyOne
的值,但 badName
的值 null
。
在这些情况下,RestTemplate
/Jackson
不抛出任何 exception/error 是否正常?
如果我想强制调用抛出异常怎么办?
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).
示例:
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,true);