Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]@JSONField(format="xxxxx") 对http请求对象的LocalDateTime字段不生效!!! #3318

Open
ldcsaa opened this issue Jan 24, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@ldcsaa
Copy link

ldcsaa commented Jan 24, 2025

问题描述

@JSONField(format="xxxxx") 对http请求对象的LocalDateTime字段不生效!!!

我感觉是:下图中,第40行和第63行代码对 yyyyMMddhhmmss19 属性进行判断是多余的,删除掉应该就好了。理论上代码应该执行第41行才对。

环境信息

  • OS信息: [e.g.:Windows]
  • JDK信息: [e.g.:Openjdk 1.17.0_13]
  • 版本信息:[e.g.:Fastjson2 2.0.54]

重现步骤

  1. 基于应用程序其他部分需要,FastJsonHttpMessageConverter的默认格式设置为 "yyyy-MM-dd HH:mm:ss.SSS"
    config.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");

  2. 某个 HTTP Request 对象的输入格式设置为 "yyyy-MM-dd HH:mm:ss"

class XxxRequest {
    /**
     * 计划开始时间
     */
    @JSONField(format = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime planStartTime;
}
  1. 实际使用过程中,@JSONField(format="xxxxx") 对http请求对象的LocalDateTime字段不生效!!!

Image

  1. 异常堆栈
Caused by: java.time.format.DateTimeParseException: Text '2025-01-25 01:29:30' could not be parsed at index 19
	at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2052) ~[?:?]
	at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1954) ~[?:?]
	at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:494) ~[?:?]
	at com.alibaba.fastjson2.JSONReader.readLocalDateTime(JSONReader.java:1320) ~[fastjson2-2.0.54.jar:?]
	at com.alibaba.fastjson2.reader.ObjectReaderImplLocalDateTime.readObject(ObjectReaderImplLocalDateTime.java:64) ~[fastjson2-2.0.54.jar:?]
	at com.alibaba.fastjson2.reader.ORG_3_14_CreateSendMailTaskRequest.readObject(Unknown Source) ~[?:?]
	at com.alibaba.fastjson2.JSON.parseObject(JSON.java:1655) ~[fastjson2-2.0.54.jar:?]
	at com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter.readType(FastJsonHttpMessageConverter.java:111) ~[fastjson2-extension-spring6-2.0.54.jar:?]
	at com.alibaba.fastjson2.support.spring6.http.converter.FastJsonHttpMessageConverter.read(FastJsonHttpMessageConverter.java:80) ~[fastjson2-extension-spring6-2.0.54.jar:?]
	at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:184) ~[spring-webmvc-6.1.15.jar:6.1.15]
	at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:161) ~[spring-webmvc-6.1.15.jar:6.1.15]
	at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:135) ~[spring-webmvc-6.1.15.jar:6.1.15]
	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122) ~[spring-web-6.1.15.jar:6.1.15]
	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:224) ~[spring-web-6.1.15.jar:6.1.15]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:178) ~[spring-web-6.1.15.jar:6.1.15]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118) ~[spring-webmvc-6.1.15.jar:6.1.15]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:926) ~[spring-webmvc-6.1.15.jar:6.1.15]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:831) ~[spring-webmvc-6.1.15.jar:6.1.15]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-6.1.15.jar:6.1.15]
	... 
	... 145 more

期待的正确结果

@JSONField(format="xxxxx") 对http请求对象的LocalDateTime字段生效!

@ldcsaa ldcsaa added the bug Something isn't working label Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant