postman提交json格式时间,不能反序列化为LocalDateTime?

postman提交的时间参数

"beginTime":"2022-12-26T9:40:00",

"endTime":"2022-12-30T23:40:00"

后端报错情况

org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2022-12-26T9:40:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2022-12-26T9:40:00' could not be parsed at index 11; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2022-12-26T9:40:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2022-12-26T9:40:00' could not be parsed at index 11

at [Source: (PushbackInputStream); line: 10, column: 17] (through reference chain: com.hmdp.entity.Voucher["beginTime"])

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:285) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.read(AbstractJackson2HttpMessageConverter.java:243) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:205) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:158) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:131) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:121) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:167) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:134) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:105) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:878) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:792) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at javax.servlet.http.HttpServlet.service(HttpServlet.java:652) ~[tomcat-embed-core-9.0.46.jar:4.0.FR]

at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.46.jar:4.0.FR]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.46.jar:9.0.46]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) ~[na:na]

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) ~[na:na]

at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.46.jar:9.0.46]

at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]

Caused by: com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2022-12-26T9:40:00": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2022-12-26T9:40:00' could not be parsed at index 11

at [Source: (PushbackInputStream); line: 10, column: 17] (through reference chain: com.hmdp.entity.Voucher["beginTime"])

at com.fasterxml.jackson.databind.exc.InvalidFormatException.from(InvalidFormatException.java:67) ~[jackson-databind-2.11.4.jar:2.11.4]

at com.fasterxml.jackson.databind.DeserializationContext.weirdStringException(DeserializationContext.java:1702) ~[jackson-databind-2.11.4.jar:2.11.4]

at com.fasterxml.jackson.databind.DeserializationContext.handleWeirdStringValue(DeserializationContext.java:947) ~[jackson-databind-2.11.4.jar:2.11.4]

at com.fasterxml.jackson.datatype.jsr310.deser.JSR310DeserializerBase._handleDateTimeException(JSR310DeserializerBase.java:129) ~[jackson-datatype-jsr310-2.11.4.jar:2.11.4]

at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:102) ~[jackson-datatype-jsr310-2.11.4.jar:2.11.4]

at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:39) ~[jackson-datatype-jsr310-2.11.4.jar:2.11.4]

at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129) ~[jackson-databind-2.11.4.jar:2.11.4]

at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:371) ~[jackson-databind-2.11.4.jar:2.11.4]

at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:164) ~[jackson-databind-2.11.4.jar:2.11.4]

at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4526) ~[jackson-databind-2.11.4.jar:2.11.4]

at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3521) ~[jackson-databind-2.11.4.jar:2.11.4]

at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.readJavaType(AbstractJackson2HttpMessageConverter.java:274) ~[spring-web-5.2.15.RELEASE.jar:5.2.15.RELEASE]

... 51 common frames omitted

Caused by: java.time.format.DateTimeParseException: Text '2022-12-26T9:40:00' could not be parsed at index 11

at java.base/java.time.format.DateTimeFormatter.parseResolved0(DateTimeFormatter.java:2051) ~[na:na]

at java.base/java.time.format.DateTimeFormatter.parse(DateTimeFormatter.java:1953) ~[na:na]

at java.base/java.time.LocalDateTime.parse(LocalDateTime.java:493) ~[na:na]

at com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer.deserialize(LocalDateTimeDeserializer.java:100) ~[jackson-datatype-jsr310-2.11.4.jar:2.11.4]

... 58 common frames omitted

实体类

 @TableField(exist = false)

private LocalDateTime beginTime;

private LocalDateTime endTime;


回答:

@TableField(exist = false)

@DateTimeFormat(pattern = "yyyy-MM-dd")

private LocalDateTime beginTime;

@DateTimeFormat(pattern = "yyyy-MM-dd")

private LocalDateTime endTime;


回答:

以SpringBoot为例

  1. 引入JSR-310的相关依赖

    <dependency>

    <groupId>com.fasterxml.jackson.datatype</groupId>

    <artifactId>jackson-datatype-jsr310</artifactId>

    </dependency>

  2. 增加配置

    spring.jackson.serialization.write-dates-as-timestamps=false
  3. 这样应该就可以正确序列化LoacalDateTime了
  4. 扩展:如果需要支持更多种的日期格式(非标准的),需要重新定义Jackson的ObjectMapper,并加入JSR310的相关序列化规则
  public static final String DEFAULT_DATETIME_MS_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";

public static final String DEFAULT_DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";

public static final String DEFAULT_TIME_PATTERN = "HH:mm:ss";

public static final String ABSOLUTE_DATETIME_MS_PATTERN = "yyyyMMddHHmmssSSS";

public static final String ABSOLUTE_DATETIME_PATTERN = "yyyyMMddHHmmss";

public static final String ABSOLUTE_DATE_PATTERN = "yyyyMMdd";

public static final String ABSOLUTE_TIME_PATTERN = "HHmmss";

public static final String DATE_TIME_LENGTH_26 = "yyyy-MM-dd+HH:mm:ss.SSSSSS";

static ObjectMapper configureObjectMapper(ObjectMapper objectMapper) {

// Set Naming Strategy

objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);

// Set Feature

objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

objectMapper.enable(MapperFeature.PROPAGATE_TRANSIENT_MARKER);

objectMapper.enable(JsonReadFeature.ALLOW_BACKSLASH_ESCAPING_ANY_CHARACTER.mappedFeature());

objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

// Handle Java Time & JSR310

registerJavaTime(objectMapper);

return objectMapper;

}

/**

* registerJavaTime

*

* <p>Handle Time Format to JSR310

*

* @author Created by ivan at 下午2:52 2020/1/10.

*/

public static void registerJavaTime(ObjectMapper objectMapper) {

// Handle Java Time

objectMapper.registerModule(new JavaTimeModule());

// Handle JSR310 Time

SimpleModule jsr310Module = new SimpleModule();

jsr310Module.addSerializer(

LocalDateTime.class,

new LocalDateTimeSerializer(

DateTimeFormatter.ofPattern(DateTimePatterns.DEFAULT_DATETIME_PATTERN)));

jsr310Module.addSerializer(

LocalDate.class,

new LocalDateSerializer(

DateTimeFormatter.ofPattern(DateTimePatterns.DEFAULT_DATE_PATTERN)));

jsr310Module.addSerializer(

LocalTime.class,

new LocalTimeSerializer(

DateTimeFormatter.ofPattern(DateTimePatterns.DEFAULT_TIME_PATTERN)));

jsr310Module.addDeserializer(

LocalDateTime.class,

new LocalDateTimeDeserializer(

DateTimeFormatter.ofPattern(DateTimePatterns.DEFAULT_DATETIME_PATTERN)));

jsr310Module.addDeserializer(

LocalDate.class,

new LocalDateDeserializer(

DateTimeFormatter.ofPattern(DateTimePatterns.DEFAULT_DATE_PATTERN)));

jsr310Module.addDeserializer(

LocalTime.class,

new LocalTimeDeserializer(

DateTimeFormatter.ofPattern(DateTimePatterns.DEFAULT_TIME_PATTERN)));

objectMapper.registerModule(jsr310Module);

}

  1. 最后通过配置Spring的HttpMessageConverters,替换掉原有的Jackson ObjectMapper。这样新的请求会根据配置的规则,进行格式判断,并进行转换。

以上是 postman提交json格式时间,不能反序列化为LocalDateTime? 的全部内容, 来源链接: utcz.com/p/944901.html

回到顶部