感谢Sotirios Delimanolis的评论,我能够解决这个问题.
1)添加
com.fasterxml.jackson.datatype
jackson-datatype-jdk8
作为依赖.
2)重新配置Jackson映射器.寄存器:
@Bean
@Primary
public ObjectMapper jacksonObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());
return mapper;
}
或者这样做以注册jdk8模块
/**
* @return Jackson jdk8 module to be registered with every bean of type
* {@link ObjectMapper}
*/
@Bean
public Module jdk8JacksonModule() {
return new Jdk8Module();
}
Another way to customize Jackson is to add beans of type com.fasterxml.jackson.databind.Module to your context. They will be registered with every bean of type ObjectMapper, providing a global mechanism for contributing custom modules when you add new features to your application.
这样做只会注册附加模块并保留Spring Boot提供的内置Jackson配置.
3)结果
现在,当发送的json中缺少该属性时,它将映射为null
(这不是那么好.我期待它会给我一个可选的,我将能够使用.isPresent()).
当它是一个空字符串(“isMeetingOrSale”:“”)时,杰克逊会返回一个错误:
Could not read JSON: Can not construct instance of
MyModel from String value ”: value not
one of declared Enum instance names: [VAL1, VAL2]
哪个看起来对我好.