Spring Boot中使用Java8新时间类LocalDateTime、LocalDate等作为传参

一、基础及问题

1.java8新时间、日期类有:LocalDate 、 LocalDateTime d、LocalTime

2.spring mvc使用LocalDate等传值时会报错:Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate'  。MVC 不能将字符串转为LocalDate。使用Date则可以接收且转换成功,猜测是mvc自己暂时不支持LocalDate

二、问题解决

1.直接配置string与LocalDate的转换器:(亲测有效)

@Configuration
public class MappingConverterAdapter {

    @Bean
    public Converter<String, LocalDate> localDateConvert() {
        return new Converter<String, LocalDate>() {
            @Override
            public LocalDate convert(String source) {
                if (source == null || source.isEmpty())
                    return null;
                int length = source.length();
                String pattern = "yyyy-MM-dd";   //对传入的值做判断,不足的自动补足为yyyy-MM-dd型
                switch (length) {
                    case 4:    //传值为yyyy
                        source += "-01-01";
                        break;
                    case 7:     //传值为yyyy-MM
                        source += "-01";
                        break;
                    default:
                        break;
                    }
                return LocalDate.parse(source, DateTimeFormatter.ofPattern(pattern));
            }
        };
    }

}

2.使用ControllerAdvice配合initBinder

@ControllerAdvice
    public class GlobalExceptionHandler {

        @InitBinder
        protected void initBinder(WebDataBinder binder) {
            binder.registerCustomEditor(LocalDate.class, new PropertyEditorSupport() {
                @Override
                public void setAsText(String text) throws IllegalArgumentException {
                    setValue(LocalDate.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                }
            });
            binder.registerCustomEditor(LocalDateTime.class, new PropertyEditorSupport() {
                @Override
                public void setAsText(String text) throws IllegalArgumentException {
                    setValue(LocalDateTime.parse(text, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                }
            });
            binder.registerCustomEditor(LocalTime.class, new PropertyEditorSupport() {
                @Override
                public void setAsText(String text) throws IllegalArgumentException {
                    setValue(LocalTime.parse(text, DateTimeFormatter.ofPattern("HH:mm:ss")));
                }
            });
        }
    }

3.当LocalDateTime作为Json形式传入时,要利用Jackson的json序列化和反序列化来做

@Configuration
    public class JacksonConfig {

        /** 默认日期时间格式 */
        public static final String DEFAULT_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
        /** 默认日期格式 */
        public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
        /** 默认时间格式 */
        public static final String DEFAULT_TIME_FORMAT = "HH:mm:ss";

        @Bean
        public ObjectMapper objectMapper(){
            ObjectMapper objectMapper = new ObjectMapper();
            JavaTimeModule javaTimeModule = new JavaTimeModule();
            javaTimeModule.addSerializer(LocalDateTime.class,new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
            javaTimeModule.addSerializer(LocalDate.class,new LocalDateSerializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
            javaTimeModule.addSerializer(LocalTime.class,new LocalTimeSerializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
            javaTimeModule.addDeserializer(LocalDateTime.class,new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT)));
            javaTimeModule.addDeserializer(LocalDate.class,new LocalDateDeserializer(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT)));
            javaTimeModule.addDeserializer(LocalTime.class,new LocalTimeDeserializer(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMAT)));
            objectMapper.registerModule(javaTimeModule).registerModule(new ParameterNamesModule());
            return objectMapper;
        }

    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值