zoukankan      html  css  js  c++  java
  • springboot 日期类型处理

    1. 日期类型输出参数处理

    默认日期格式只支持:2017-07-01T10:10:01

    修改为2017-07-01 10:10:01

    只需要修改配置文件即可:

    spring:
      jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: GMT+8

    1. 日期类型输入参数处理

    默认日期格式只支持:2017/07/01

    修改为支持2017-07-01日期格式

    @ControllerAdvice
    public class GlobalHandler {
    
        @InitBinder
        public void initBinder(WebDataBinder binder) {
    
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    
        }
    }

    看到网友介绍的可以使用registerCustomEditor的重载方法,使用日期类型支持多种格式,但好像是需要在具体的日期字段上添加注解才可行,我没有尝试。

    还有网友介绍的:通过继承WebMvcConfigurationSupport

    @Configuration 
    public class WebMvcConfig extends WebMvcConfigurationSupport {
    
        /**
         * 添加自定义的Converters和Formatters.
         */
        @Override
        protected void addFormatters(FormatterRegistry registry) {
           registry.addConverter(new StringToDateConverter());
        }
    }

    这种方式会使用application.yml对日期的配置失效。

    为了解决配置失效的问题,尝试实现WebMvcConfigurer,但又报swagger的错误。

    所以最后选择了上述的方案。

  • 相关阅读:
    Serverless
    Kubernetes
    下一代微服务-ServiceMesh
    SOA服务治理
    微服务架构
    RPC之Thrift
    【MySQL】MySQL高可用架构之MHA
    远程通信的几种选择(RPC,Webservice,RMI,JMS的区别)
    Redis与Memcached的区别
    LVS简介
  • 原文地址:https://www.cnblogs.com/hankuikui/p/10172691.html
Copyright © 2011-2022 走看看