【发布时间】:2018-08-14 18:56:21
【问题描述】:
我有一个带有多个 @RestController 类的 Spring Boot Web 应用程序。
我喜欢我的 REST 控制器返回的默认 json 格式。
为了在我的 DAO bean(进行 json 序列化和反序列化)中使用,我创建了一个自定义 ObjectMapper:
@Configuration
public class Config{
@Bean
public ObjectMapper getCustomObjectMapper() {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
objectMapper.setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy());
return objectMapper;
}
}
在我的每个 DAO 类中,我都会自动装配我的自定义 ObjectMapper:
@Repository
@Transactional
public class MyDaoImpl implements MyDao {
@Autowired
ObjectMapper objectMapper
//Dao implementation...
}
这一切都很好。问题是我的自定义 ObjectMapper 被 Spring 自动拾取并用于序列化 REST 响应。
这是不可取的。对于 REST 控制器,我想保留 Spring 默认创建的 ObjectMapper。
我如何告诉 Spring Boot不 检测和不 使用我的自定义 ObjectMapper bean 进行自己的内部工作?
【问题讨论】:
标签: java spring-boot jackson