Spring Boot 全局日期格式化(基于注解)

2年前 (2022) 程序员胖胖胖虎阿
240 0 0

点击上方 Java后端选择 设为星标

优质文章,及时送达


学习目标

快速学会通过注解 @JsonComponent自定义日期格式化的序列化器。

快速查阅

专题阅读:《SpringBoot 布道系列》:https://www.jianshu.com/p/964370d9374e

源码下载:SpringBoot Date Format Anno:https://github.com/yizhiwazi/springboot-socks

使用教程

根据官方文档 Custom JSON Serializers and Deserializers ,想要接管Jackson的JSON的序列化和反序列化,只需通过注解 @JsonComponent来声明其静态内部类即可。

首先根据项目要求提供自定义的日期序列化器和反序列化器,其中包括:

  • DateJsonSerializerextendsJsonSerializer<Date> 表示将Date格式化为日期字符串。
  • DateJsonDeserializerextendsJsonDeserializer<Date> 表示将日期字符串解析为Date日期。
/**
 * 全局日期格式化
 */

@JsonComponent
public class DateFormatConfig {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    /**
     * 日期格式化
     */

    public static class DateJsonSerializer extends JsonSerializer<Date> {
        @Override
        public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            jsonGenerator.writeString(dateFormat.format(date));
        }
    }

    /**
     * 解析日期字符串
     */

    public static class DateJsonDeserializer extends JsonDeserializer<Date> {
        @Override
        public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            try {
                return dateFormat.parse(jsonParser.getText());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
然后提供相应的测试信息,这里以查询用户为例:


/**
 * 查询用户信息
 */

@RestController
public class UserController {
    @GetMapping("/")
    public User get() {
        return new User("1", "socks", "123456", new Date(), "GMT");
    }
}

/**
 * 用户信息
 */

public class User {

    private String userId;
    private String username;
    private String password;
    private Date createTime;
    private String timezone;

    public User(String userId, String username, String password, Date createTime, String timezone) {
        this.userId = userId;
        this.username = username;
        this.password = password;
        this.createTime = createTime;
        this.timezone = timezone;
    }

   //省略getters&setters
}
大功告成,接下来启动应用并访问 http://127.0.0.1:8080 ,可以拿到正确结果:
{
  "userId": "1",
  "username": "socks",
  "password": "123456",
  "createTime": "2018-12-26 01:03:25"
}
除了日期格式化解析之外,我们还可以在 
DateFormatConfig
 注入业务变量,例如根据当前登录人的所属时区(虽然 
SimpleDateFormat
默认读取了当地时区,但在实际的国际化系统中,用户的所属时区是指其在系统录入的所属时区,而不是指当地时区。例如Tony这个用户账号挂在GMT+0时区,但此时他出差在香港使用,系统仍需要按照GMT+0时区来显示时间),为了解决这个问题,此时我们可以在 
DateFormatConfig
 注入当前登录人然后改变日期工具类的 
TimeZone
来动态修改时区。
根据当前登录人动态展示时区:
/**
 * 全局日期格式化
 */

@JsonComponent
public class DateFormatConfig {

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss Z") {
        @Override
        public Date parse(String source) throws ParseException {
            try {
                if (StringUtils.isEmpty(source)) {
                    return null;
                }
                return super.parse(source);
            } catch (Exception e) {
                return new StdDateFormat().parse(source);
            }
        }
    };

    private static UserController userController;//这里是指获取当前登录人的工具类

    @Autowired
    public void setUserController(UserController userController) {
        DateFormatConfig.userController = userController;
    }

    /**
     * 日期格式化
     */

    public static class DateJsonSerializer extends JsonSerializer<Date> {
        @Override
        public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            //获取当前登录人的所属时区
            dateFormat.setTimeZone(TimeZone.getTimeZone(userController.get().getTimezone()));
            //格式化日期
            jsonGenerator.writeString(dateFormat.format(date));
        }
    }

    /**
     * 解析日期字符串
     */

    public static class DateJsonDeserializer extends JsonDeserializer<Date> {
        @Override
        public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
            try {
                //获取当前登录人的所属时区
                dateFormat.setTimeZone(TimeZone.getTimeZone(userController.get().getTimezone()));
                //解析日期
                return dateFormat.parse(jsonParser.getText());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        }
    }
}
修改完后重新启动应用并访问 http://127.0.0.1:8080 ,可以拿到正确结果:
{
  "userId": "1",
  "username": "socks",
  "password": "123456",
  "createTime": "2018-12-25 17:35:50 +0000",
  "timezone": "GMT"
}

公众号 Java后端 发布的关于 Spring Boot 相关的文章,我整理成了 PDF ,关注公众号 Java后端 ,回复 666 下载。

小结

1、使用注解 
@JsonComponent
 可以快速自定义日期格式化的序列化器,免除传统通过模块注册的烦恼。
2、使用注解 
@JsonComponent
 实现与当地无关的动态时区的精髓就在于将获取当前等人的方法写在解析日期和格式化日期的代码里。
3、使用注解 
@JsonComponent
 是直接处理String和Date的相互转换的,所以要注意空串问题。例如dateFormat.parse()要预防空串。
作者:yizhiwazi
链接:https://www.jianshu.com/p/f4654d251104



-END-

如果看到这里,说明你喜欢这篇文章,请 转发、点赞。同时 标星(置顶)本公众号可以第一时间接受到博文推送。

1. 强烈推荐 16 款 IDEA 插件

2. 30 分钟学会如何使用 Shiro

3. 自己手撸一个 Spring MVC

4. Spring Boot+Redis实现接口自动幂等

Spring Boot 全局日期格式化(基于注解)


最近整理一份面试资料
《Java技术栈学习手册》
,覆盖了Java技术、面试题精选、Spring全家桶、Nginx、SSM、微服务、数据库、数据结构、架构等等。

获取方式:点“ 在看,关注公众号 Java后端 并回复 777 领取,更多内容陆续奉上。

喜欢文章,点个在看 Spring Boot 全局日期格式化(基于注解)

本文分享自微信公众号 - Java后端(web_resource)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

版权声明:程序员胖胖胖虎阿 发表于 2022年10月9日 上午12:48。
转载请注明:Spring Boot 全局日期格式化(基于注解) | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...