javascript - java日期数据前后台传递
问题描述
是这样的,我前台用了ant-design的控件传入日期类型“2017-5”,后台我用LocalDate类型接收,结果报错了
Field error in object ’arrearsStatisticsQueryInfo’ on field ’statisticalMonth’: rejected value [2017-05]; codes [typeMismatch.arrearsStatisticsQueryInfo.statisticalMonth,typeMismatch.statisticalMonth,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [arrearsStatisticsQueryInfo.statisticalMonth,statisticalMonth]; arguments []; default message [statisticalMonth]]; default message [Failed to convert property value of type [java.lang.String] to required type [java.time.LocalDate] for property ’statisticalMonth’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDate] for value ’2017-05’; nested exception is java.time.format.DateTimeParseException: Text ’2017-05’ could not be parsed at index 2]-/api/order/arrearsStatisticsorg.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errorsField error in object ’arrearsStatisticsQueryInfo’ on field ’statisticalMonth’: rejected value [2017-05]; codes [typeMismatch.arrearsStatisticsQueryInfo.statisticalMonth,typeMismatch.statisticalMonth,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [arrearsStatisticsQueryInfo.statisticalMonth,statisticalMonth]; arguments []; default message [statisticalMonth]]; default message [Failed to convert property value of type [java.lang.String] to required type [java.time.LocalDate] for property ’statisticalMonth’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [java.time.LocalDate] for value ’2017-05’; nested exception is java.time.format.DateTimeParseException: Text ’2017-05’ could not be parsed at index 2] at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:113) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:78) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:162) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129) ~[spring-web-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:814) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:737) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893) ~[spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:969) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:860) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) [tomcat-embed-core-8.0.30.jar:8.0.30] at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:845) [spring-webmvc-4.2.4.RELEASE.jar:4.2.4.RELEASE] at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) [tomcat-embed-core-8.0.30.jar:8.0.30]
我的后台怎么样才能接收呢?其实得到2017-5-1这一天也可以
问题解答
回答1:用string接收然后转成时间格式
回答2:谢邀, 看日志应该是String转LocalDate类型没有指定格式的错误
在statisticalMonth这个field上加上org.springframework.format.annotation.@DateTimeFormat
回答3:时间数据序列化比较麻烦,用Date对象接收也行,需要做一些配置,用string或long比较省心!
相关文章:
1. android - 求 360浏览器 百度浏览器 搜狗浏览器的最新启动类名2. html5 - HTML代码中的文字乱码是怎么回事?3. html按键开关如何提交我想需要的值到数据库4. Android 高德地图如何实现拖动平移地图时地图上的小图标不动?5. css3动画 - 实现css3推倒动画6. css - 前端flex布局嵌套内层的布局不起作用?7. css - ul ol前边的标记如何调整样式呢8. javascript - 微信小程序 wx.downloadFile下载文件大小有限制吗9. javascript - 在html中写了个<video>视频,浏览器却是一片空白为什么呢?360浏览器?10. javascript - 如何通过参数获取Object的子节点?