更新时间:2023-01-17 10:21:31
开始使用3.1 M1,您可以通过 mvc:annotation-driven
的子元素注册 HttpMessageConverters
来指定jackson自定义配置。
Starting with 3.1 M1 you can specify jackson custom configuration by registering an HttpMessageConverters
through a sub-element of mvc:annotation-driven
.
参见 SPR-7504 更容易添加新内容消息转换器到AnnotationMethodHandlerAdapter
See SPR-7504 Make it easier to add new Message Converters to AnnotationMethodHandlerAdapter
例如:
<bean id="jacksonObjectMapper" class="x.y.z.CustomObjectMapper">
</bean>
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="objectMapper" ref="jacksonObjectMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
CustomObjectMapper对象
The CustomObjectMapper Object
@Component("jacksonObjectMapper")
public class CustomObjectMapper extends ObjectMapper {
@PostConstruct
public void afterPropertiesSet() throws Exception {
SerializationConfig serialConfig = getSerializationConfig()
.withDateFormat(null);
//any other configuration
this.setSerializationConfig(serialConfig);
}
}
SerializationConfig。 withDateFormat
SerializationConfig .withDateFormat
除了使用指定的日期格式构建实例外,还将启用或禁用
Feature.WRITE_DATES_AS_TIMESTAMPS(如果格式设置则启用as null;如果非null则禁用)
In addition to constructing instance with specified date format, will enable or disable Feature.WRITE_DATES_AS_TIMESTAMPS (enable if format set as null; disable if non-null)