java - MappingJackson2HttpMessageConverter 在 Spring 4 启动期间多次实例化覆盖(无效)自定义 ObjectMapper 设置

标签 java spring spring-mvc objectmapper

我正在尝试将我的 ObjectMapper 配置为具有特定时区而不是使用默认 GMT。我按照来自不同地方的说明(请参阅下面的引用资料)尝试通过代码和 XML 进行配置。调试后我注意到代码确实设置了 ObjectMapper 的 TimeZone 设置一次,但是在启动期间 Jackson2ObjectMapperBuilder 类被多次调用并覆盖(null'ing)我的 TimeZone 设置并在启动过程完成时返回默认 GMT,两者在 Tomcat 和我的单元测试中。

部分代码:

配置:

@Configuration
@EnableWebMvc
public class JacksonMapperConfiguration extends WebMvcConfigurerAdapter {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(converter());
    }

    @Bean    
    public MappingJackson2HttpMessageConverter converter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter(jacksonObjectMapper());
        return converter;
    }

    @Bean
    public ObjectMapper jacksonObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        return objectMapper;
    }

    @Bean
    public Jackson2ObjectMapperBuilder jacksonBuilder() {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.timeZone("America/New_York");
        return builder;
    }
}

实体:

@Entity
public class AllocationEstimate extends AbstractEntity<Integer> {

    private static final long serialVersionUID = 1L;

    @NotNull(message = "validation.mandatoryField")
    @Column(nullable = false)
    @Temporal(TemporalType.DATE)
    @JsonFormat(pattern = "MM/dd/yyyy")
    private Date endDate;

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
}

Controller :

@RestController
@RequestMapping("/allocation-estimate")
public class AllocationEstimateRestController extends AbstractGenericCrudRestController<AllocationEstimate, Integer> {

    private final AllocationEstimateService allocationEstimateService;

    @Autowired
    public AllocationEstimateRestController(AllocationEstimateService allocationEstimateService) {
        this.AllocationEstimateService = allocationEstimateService;
    }

    @Override
    @Post
    public AllocationEstimate createOrUpdate(@RequestBody @Valid AllocationEstimate resource) {
        return super.createOrUpdate(resource);
    }
}

我已经使用了以上所有内容以及不同设置的组合。有些只有 configureMessageConverters,或者只是试图设置一个 @Bean。

我的问题是是否有人看到过这种行为,我该如何解决?我尝试了不同的组合,但我的设置总是在启动过程结束时被覆盖。我正在使用 Spring 4.3.16。

供引用:

Configuring ObjectMapper in Spring

https://dzone.com/articles/latest-jackson-integration

https://www.javacodegeeks.com/2014/09/customizing-httpmessageconverters-with-spring-boot-and-spring-mvc.html

How to force Jackson2ObjectMapperBuilder in spring-boot?

SpringMVC Jackson2HttpMessageConverter customization doesn't work

编辑 我怀疑 AllEncompassingFormHttpMessageConverter 是罪魁祸首,我不确定它是什么,但某些东西多次实例化此类,这又多次实例化 MappingJackson2HttpMessageConverter,从而覆盖我在配置中的转换器。

最佳答案

@Primary 添加到您的 jacksonObjectMapper()jacksonBuilder() beans。

我认为 Spring 尝试初始化一些隐藏的 ObjectMapper 由不同的 jar 拉入,而主要的不是你的。

关于java - MappingJackson2HttpMessageConverter 在 Spring 4 启动期间多次实例化覆盖(无效)自定义 ObjectMapper 设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50352525/

相关文章:

hibernate - Elasticsearch 的性能是否会受到关系数据库模型(如MySQL)的影响?

spring - 我可以将 Spring MVC servlet 的 WebApplicationContext 注入(inject) DelegatingFilterProxy 吗?

java - 用于计算字符串匹配条件的 OQL 语法

java - WebLogic javax.net.ssl.SSLHandshakeException

java - 结合 DDD、多种实现和 REST

java - 如何在使用 Spring Data JPA nativeQuery 时用参数值替换表名称

java - 没有回复监听器的 Spring Integration JMS 网关

java - 无法使用 spring Rest Controller 下载静态 xml 文件

spring - 如何绕过重置 session 的到期时间

java - testng - 如何分离测试和支持方法?