问题描述:
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot coerce empty String ("") to `com.cxstar.business.entity.enums.ShelveStatus` value (but could if coercion was enabled using `CoercionConfig`); nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot coerce empty String ("") to `com.cxstar.business.entity.enums.ShelveStatus` value (but could if coercion was enabled using `CoercionConfig`)
at [Source: (PushbackInputStream); line: 1, column: 79] (through reference chain: com.cxstar.api.param.vo.DataRequest["states"])
问题原因:
传入的是空字符串,不能与枚举类型相匹配导致报错
解决方案:
注入ObjectMapper ,并修改对应配置为不匹配时设置为null
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private ObjectMapper objectMapper;
@PostConstruct
public void EnumObjectMapper() {
// 解决enum不匹配问题 默认值为false
objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true);
}
}