• java.lang.IllegalStateException: No primary or default constructor found for class java.time.LocalDate


    转载自:https://blog.csdn.net/Coder_Arley/article/details/81910705

    springboot中报错如下:

    springmvc也可以使用类似处理方法。其他参考:SrpingMVC通过JSON注入from数据到实体自定义(LocalDateTime,LocalDate,Boolean类型)字段的序列化、反序列化方法

    java.lang.IllegalStateException: No primary or default constructor found for class java.time.LocalDate
        at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:212)
        at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:84)
        at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:132)
        at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:124)
        at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:161)
        at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:131)
        at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
        at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
        at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
        at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991)
        at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925)
        at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974)

    解决方法:在maven添加如下依赖

    让框架可以处理jsr310的新日期、时间类

    <dependency>
         <groupId>com.fasterxml.jackson.datatype</groupId>
         <artifactId>jackson-datatype-jsr310</artifactId>
         <version>2.9.9</version>
    </dependency>

    任意一个带Configuration注解的类中添加以下代码

      @Bean
        public Converter<String, String> StringConvert() {
            return new Converter<String, String>() {
                @Override
                public String convert(String source) {
                    return StringUtils.trimToNull(source);
                }
            };
        }
     
     
     
        @Bean
        public Converter<String, LocalDate> LocalDateConvert() {
            return new Converter<String, LocalDate>() {
                @Override
                public LocalDate convert(String source) {
                    if (StringUtils.isBlank(source)) {
                        return null;
                    }
                    return LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
                }
     
            };
        }
     
        @Bean
        public Converter<String, LocalDateTime> LocalDateTimeConvert() {
            return new Converter<String, LocalDateTime>() {
                @Override
                public LocalDateTime convert(String source) {
                    if (StringUtils.isBlank(source)) {
                        return null;
                    }
                    return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
                }
     
            };

    Controller里面添加注解@RequestParam(required=false)

    @RequestMapping(value = "/test")
        @ResponseBody
        public String test(@RequestParam(required = false) LocalDate startDate) {
            System.out.println(startDate);
            return "success";
        }
  • 相关阅读:
    Ajax实现文件下载
    jquery easyui 插件开发
    Chrome谷歌浏览器首页被改为Hao123导航怎么办|附各类解决方法【转】
    查看mysql版本的四种方法
    IntelliJ IDEA 快捷键大全
    Java中判断字符串是否为数字的五种方法
    比数据分析更要命的是:数据质量
    Python绘制六种可视化图表详解,三维图最炫酷!你觉得呢?
    大数据需要好设计
    Python模块学习filecmp文件比较
  • 原文地址:https://www.cnblogs.com/passedbylove/p/11582208.html
Copyright © 2020-2023  润新知