Spring GET请求实体中日期的转换


一般而言,在参数较少的时候,会使用@Requestparam将参数绑定到对应的get请求中,

但是,如果接收的参数过多,则使用实体较好,这就涉及到一个新的问题,使用get请求时,如何解析string到对应的Date格式呢?

 

spring中提供了全局的参数配置,可以完成日期的转换:

@ControllerAdvice
public class LocalDateControllerAdvice {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                // 日期解析方法,将对应的text解析成自己需要的格式
                this.setValue(DateUtil.parse(text); 
    } });
  }
}

 

参考资料:

1. https://blog.codecentric.de/en/2017/08/parsing-of-localdate-query-parameters-in-spring-boot/

2. https://stackoverflow.com/nocaptcha?s=6d25a2ea-9429-4ec1-a2b1-f4da279ac7b9

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM