java.lang.IllegalStateException:Noprimaryorde。。。springboot中报错如下:
springmvc也可以使⽤类似处理⽅法。其他参考:
java.lang.IllegalStateException: No primary or default constructor found for class java.time.LocalDate
at org.hod.ateAttribute(ModelAttributeMethodProcessor.java:212)
at org.springframework.web.hod.ateAttribute(ServletModelAttributeMethodProcessor.java:84)    at org.hod.solveArgument(ModelAttributeMethodProcessor.java:132)
at org.hod.solveArgument(HandlerMethodArgumentResolverComposite.java:124)    at org.hod.MethodArgumentValues(InvocableHandlerMethod.java:161)
at org.hod.support.InvocableHandlerMethod.invokeForRequest(InvocableHand
lerMethod.java:131)
at org.springframework.web.hod.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102)
at org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:877)
at org.springframework.web.hod.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:783)
at org.springframework.web.hod.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) {
imToNull(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>() {
@Overridespring framework组件
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";
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。