【发布时间】:2019-04-21 11:48:48
【问题描述】:
在迁移到 Spring Boot 2.1.0 之前,我通过扩展 WebMvcConfigurerAdapter 配置了以下 ObjectMapper,按预期工作。
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
private static final ObjectMapper objectMapper = buildObjectMapper();
private static ObjectMapper buildObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.configOverride(LocalDate.class).
setFormat(JsonFormat.Value.forPattern("yyyy-MM-dd"));
mapper.registerModule(new JavaTimeModule());
return mapper;
}
public static ObjectMapper getObjectMapper() {
return objectMapper;
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(new MappingJackson2HttpMessageConverter(objectMapper)); // JSON converter
}
}
但是在迁移之后我可以看到设置 PropertyNamingStrategy 似乎没有任何效果,我的应用程序仍然希望 JSON 字段的格式与默认值匹配(例如,如预期的那样需要“bloodType”而不是“blood-type”)。
我认为映射器本身没有任何问题,因为我使用静态getObjectMapper 通过RestAssuredConfig 与REST Assured 共享同一个实例,并且如果我更新,似乎仍然按预期运行我的自定义映射器到默认行为(评论setPropertyNamingStrategy)一切似乎都按预期工作。
编辑 1
事实上,我的整个映射器都被忽略了,我尝试更改LocalDate 格式,也没有任何区别。更新标题以更好地反映问题。
【问题讨论】:
-
我认为你的映射器 propertyNamingStrategy 不是通过 Spring Boot 应用程序获取的,而是从休眠 propertyNamingStrategy 获取的,你应该试试这个。 spring.jpa.hibernate.naming.physical-strategy=com.example.MyPhysicalNamingStrategy 参考:docs.spring.io/spring-boot/docs/current/reference/html/…
标签: java spring-mvc spring-boot jackson