如何在Spring-MVC中跳过杰克逊时区校正? [英] How to skip jackson timezone correction in spring-mvc?

查看:251
本文介绍了如何在Spring-MVC中跳过杰克逊时区校正?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想配置jackson以使用以下格式输出任何日期/时间值:

I want to configure jackson to output any date/time values with the following format:

spring.jackson.date-format=yyyy-MM-dd'T'HH:mm:ss

我要获取许多数据库行,并将它们作为json映射返回.

I'm fetching many database rows and return them just as a json map.

@RestController
public class MyService {
    @GetMapping
    public List<Map<String, Object>> get(Param params) {
             return jdbcTemplate.queryForList(sql, params);
    }
}

问题:数据库和jvm的默认时区为Europe/Berlin,因此为UTC + 2.因此,杰克逊会自动将所有数据库接收的java.sql.Timestamp首先转换为UTC(减去2小时),然后通过json输出它们.

Problem: the databases and jvm default timezone is Europe/Berlin, thus UTC+2. Therefor jackson automatically converts any database-received java.sql.Timestamp to UTC first (subtracts 2 hours), and then outputs them via json.

mysql数据库本身中,它是datetime类型.

In the mysql database itself, it's a datetime type.

但是我只想让杰克逊按原样输出时间戳,而无需事先转换!可以跳过时区校正吗?

But I just want jackson to output the timestamps "as is", without prior conversion! Is that possible to skip timezone correction?

我只想忽略时区而不进行对话.剪下来.

I just want to ignore the timezone without conversation. Just cut it.

推荐答案

最后,事实证明,简单的方法是将杰克逊ObjectMapper(默认情况下使用UTC)时区设置为jvm默认值:

Finally it turned out the simples way is to just set the jacksons ObjectMapper (which uses UTC by defaut) timezone to the jvm defaults:

@Bean
public Jackson2ObjectMapperBuilderCustomizer init() {
    return new Jackson2ObjectMapperBuilderCustomizer() {
        @Override
        public void customize(Jackson2ObjectMapperBuilder builder) {
            builder.timeZone(TimeZone.getDefault());
        }
    };
}

如果有人知道我如何通过使用spring.jackson.time-zone application.property可以达到相同的效果,我将不胜感激.

I'd appreciate if anybody knows how I can achieve the same by just using the spring.jackson.time-zone application.property.

这篇关于如何在Spring-MVC中跳过杰克逊时区校正?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆