时间字符串作为普通请求参数传入时,转换用的是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