springboot,get传日期格式转换

简介: springboot,get传日期格式转换

男女双方愿意相互观察是爱情的第一征象——瓦西列

对于这种请求:

http://api.achao.cn/example?date=2022-11-09

我们可以配置转换器,mvc则会自动帮我们转

import cn.hutool.core.date.DatePattern;
import io.github.vampireachao.stream.core.optional.Sf;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
/**
 * @author <achao1441470436@gmail.com>
 * @since 2022/4/28 18:30
 */
@Configuration
public class ConvertConfig {
    @Autowired
    public <T, R> ConvertConfig(ConversionService conversionService, List<Converter<T, R>> converters) {
        GenericConversionService genericConversionService = (GenericConversionService) conversionService;
        genericConversionService.addConverter(new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String source) {
                return Sf.ofStr(source).mayLet(text -> LocalDate.parse(text, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN))).get();
            }
        });
        genericConversionService.addConverter(new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String source) {
                return Sf.ofStr(source).mayLet(text -> LocalDateTime.parse(text, DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN))).get();
            }
        });
        converters.forEach(genericConversionService::addConverter);
    }
}
相关文章
|
5月前
|
Java 数据库
SpringBoot中如何在过滤器中取get的参数值
SpringBoot中如何在过滤器中取get的参数值
148 0
|
11月前
|
Java
Springboot接口同时支持GET和POST请求
Springboot接口同时支持GET和POST请求
596 0
|
7天前
|
JavaScript 前端开发 Java
SpringBoot项目的html页面使用axios进行get post请求
SpringBoot项目的html页面使用axios进行get post请求
22 6
|
5月前
|
监控 IDE Java
Java项目调试实战:如何高效调试Spring Boot项目中的GET请求,并通过equalsIgnoreCase()解决大小写不一致问题
Java项目调试实战:如何高效调试Spring Boot项目中的GET请求,并通过equalsIgnoreCase()解决大小写不一致问题
86 0
|
11月前
|
Java 程序员
在Springboot HandlerInterceptor中获取GET和POST请求参数
上面RequestWrapper的代码我查阅资料的时候在多篇博文中看到了,但是单有RequestWrapper还不足以完成整个请求,而且我看很多网络上的博客都是只在Interceptor中Wapper,但实际这样是不对的,而且也完全不需要,因为必须要替换掉整个请求链路中的Request才行。这里我们只需要在Filter中将普通的Request替换成我们自己的RequestWrapper
373 0
SpringBoot-29-RestTemplate的Get请求使用详解
SpringBoot-29-RestTemplate的Get请求使用详解
80 0
SpringBoot-29-RestTemplate的Get请求使用详解
|
Java
【SpringBoot】WebMvcConfigurer实现类不被加载(o.s.web.servlet.PageNotFound : No mapping for GET)的问题解决
【SpringBoot】WebMvcConfigurer实现类不被加载(o.s.web.servlet.PageNotFound : No mapping for GET)的问题解决
848 0
SpringBoot自动装配加载过程全梳理!你能get多少?
首先对于一个SpringBoot工程来说,最明显的标志的就是 @SpringBootApplication它标记了这是一个SpringBoot工程,所以今天的 SpringBoot自动装配原理也就是从它开始说起。
|
Java
springboot 接收post、get、重定向,并从url中获取参数
springboot 接收post、get、重定向,并从url中获取参数
868 0
|
Java Maven 微服务
SpringBoot系列教程web篇之Get请求参数解析姿势汇总
一般在开发web应用的时候,如果提供http接口,最常见的http请求方式为GET/POST,我们知道这两种请求方式的一个显著区别是GET请求的参数在url中,而post请求可以不在url中;那么一个SpringBoot搭建的web应用可以如何解析发起的http请求参数呢? 下面我们将结合实例汇总一下GET请求参数的几种常见的解析姿势
763 0
SpringBoot系列教程web篇之Get请求参数解析姿势汇总
下一篇
无影云桌面