首页 > 其他分享 >sping boot使用LocalDate和LocalDateTime当入参时,报缺少必要参数

sping boot使用LocalDate和LocalDateTime当入参时,报缺少必要参数

时间:2022-08-23 16:23:54浏览次数:114  
标签:return String sping boot source 参时 import public Converter

时间字符串作为普通请求参数传入时,转换用的是Converter

增加一个时间转换的配置类

import com.sjaco.lccloud.common.pay.kit.DateKit;
import com.sjaco.lccloud.common.support.DateTimeKit;
import com.sjaco.lccloud.common.utils.ObjectUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignFormatterRegistrar;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
public class LocalDateConvertConfig {

    private static Logger log = LoggerFactory.getLogger(LocalDateConvertConfig.class);


    @Bean
    public Converter<String, LocalDate> localDateConvert() {

        return new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String source) {
                if(ObjectUtil.isEmpty(source)){
                    return null;
                }
                LocalDate dateTime = null;
                String str=String.valueOf(source);

                DateTimeFormatter df = DateTimeFormatter.ofPattern(DateKit.datePattern);

                try {
                    dateTime = LocalDate.parse(str, df);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
                return dateTime;
            }

        };
    }

    @Bean
    public Converter<String, LocalDateTime> localDateTimeConvert() {

        return new Converter<String, LocalDateTime>() {
            @Override
            public LocalDateTime convert(String source) {
                if(ObjectUtil.isEmpty(source)){
                    return null;
                }
                LocalDateTime dateTime = null;
                String str=String.valueOf(source);

                DateTimeFormatter df = DateTimeFormatter.ofPattern(DateTimeKit.NORM_DATETIME_PATTERN);

                try {
                    dateTime = LocalDateTime.parse(str, df);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
                return dateTime;
            }

        };
    }

    @Bean
    public FeignFormatterRegistrar localDataTimeFormatRegister() {
        return registry -> registry.addConverter(new LocalDateTime2StringConverter());
    }

    @Bean
    public FeignFormatterRegistrar localDataFormatRegister() {
        return registry -> registry.addConverter(new LocalDate2StringConverter());
    }


    private static class LocalDateTime2StringConverter implements Converter<LocalDateTime, String> {

        @Override
        public String convert(LocalDateTime source) {
            return source.format(DateTimeFormatter.ofPattern(DateTimeKit.NORM_DATETIME_PATTERN));
        }

    }

    private static class LocalDate2StringConverter implements Converter<LocalDate, String> {
        @Override
        public String convert(LocalDate source) {
            return source.format(DateTimeFormatter.ofPattern(DateKit.datePattern));
        }
    }

}

 

标签:return,String,sping,boot,source,参时,import,public,Converter
From: https://www.cnblogs.com/stromgao/p/16616673.html

相关文章

  • springboot中配置文件的读取顺序
    springboot中配置文件的加载顺序1.简介 在一个springboot项目中是可以存在多个配置文件的,那这些配置文件的所在位置以及具体内容的不同会影响他们被springboot加载的优......
  • SpringBoot excel文件下载
    Filefile=newFile(xxx);response.setCharacterEncoding("utf-8");response.addHeader("Content-Disposition","attachment;filename*=UTF-8''"+URLEncoder.encod......
  • spring boot的静态文件
    原理:基于http协议获取远程文件实现:远程为HTTP服务器,浏览器发出请求即可基于SpringBoot下载静态文件,tomcat作为http服务器,从配置的角度完成两步即可 第一步:spring.......
  • IDEA中用Maven构建Spring Boot项目
    第一步,创建一个Maven项目第二步,配置pom.xml文件添加父依赖 <parent><artifactId>spring-boot-dependencies</artifactId><groupId>org.springfram......
  • springboot~elasticsearch对nested集合类型的字段进行不等于的检索
    对于es的数据类型来说,如果它是一个复杂类型,而我们需要把复杂类型进行检索,那么应该定义成nested类型,而对于它的检索,如果是非集合数据,它与其它类型没有分别;而如果你的nested......
  • 轻量级分布式任务调度平台(XXL-JOB介绍、原理、工作流程、XXL-JOB环境搭建集成springb
    轻量级分布式任务调度平台(一、XXL-JOB介绍、原理、工作流程)XXL-JOB#【轻量级分布式任务调度平台】(1)基本介绍#XXL-JOB是一个轻量级分布式任务调度平台,主打特点是......
  • U-Boot学习
    U-Boot,全程UniversalBootLoader,是BootLoader的一种,BootLoader就是在操作系统运行之前执行的一段小程序。对于ZYNQ而言,在引导过程中,分两步:一、先运行FSBL来设置PS二、......
  • Jenkins+Docker 一键自动化部署 SpringBoot 项目
    实现最简单全面的Jenkins+docker+springboot 一键自动部署项目,步骤齐全,少走坑路。环境:centos7+git(gitee)简述实现步骤:在docker安装jenkins,配置jenkins基本信息,利用Do......
  • springboot整合mybatis-plus实现增删改查功能
    一、创建数据库字段名称中文类型长度主键自增默认值备注Id Int Y   emp_name员工姓名varchar ......
  • SpringBoot-自动配置
    1.什么是自动配置?SpringBoot自动配置,英文名是Auto-Configuration:    是指基础我们引入的依赖jar包,对SpringBoot应用进行自动配置为SpringBoot框架的“开箱......