Environment: springboot 2.0 9 (this problem does not exist in the latest version of springboot)
Problem Description:
Prompt when post requests @requestbody to receive objects
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized field "aaaaa" (class com.ex.application.model.RiskAudit), not marked as ignorable; nested exception is com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "aaaaa" (class com.ex.application.model.RiskAudit), not marked as ignorable (25 known properties: "xxx"])
at [Source: (PushbackInputStream); line: 4, column: 15] (through reference chain: com.ex.application.model.RiskAudit["aaaaa"])
AAAAA is not a property of the riskaudit object
Problem Fix:
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder;
/**
* Ignore unmatched properties or add @JsonIgnoreProperties(ignoreUnknown = true) annotation to the response object
* @param converters
*/
@Bean
public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
converter.setObjectMapper(objectMapper);
return converter;
}
/**
* Format the date (I used LocalDateTime locally, LocalDateTime will become an object when data is obtained without this method)
* @param converters
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ObjectMapper mapper = jackson2ObjectMapperBuilder.build();
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
converters.add(0, new MappingJackson2HttpMessageConverter(mapper));
}
}