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

轉載自:https://blog.csdn.net/Coder_Arley/article/details/81910705html

springboot中報錯以下:java

springmvc也可使用相似處理方法。其餘參考:SrpingMVC經過JSON注入from數據到實體自定義(LocalDateTime,LocalDate,Boolean類型)字段的序列化、反序列化方法web

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添加以下依賴spring

讓框架能夠處理jsr310的新日期、時間類springboot

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

任意一個帶Configuration註解的類中添加如下代碼mvc

 @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)app

@RequestMapping(value = "/test") @ResponseBody public String test(@RequestParam(required = false) LocalDate startDate) { System.out.println(startDate); return "success"; }
相關文章
相關標籤/搜索