springboot LocalDateTime 反序列化

柔情只为你懂 2023-02-27 05:58 105阅读 0赞

springboot LocalDateTime 反序列化

@RequestBody读取pojo中LocalDateTime属性字段,字符串格式为”yyyy-MM-dd HH:mm:ss”时反序列化失败

*******************

示例

***************

pojo 层

Order

  1. @Data
  2. public class Order {
  3. private String orderId;
  4. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  5. private LocalDateTime orderTime;
  6. private Double price;
  7. private Integer amount;
  8. }

***************

controller 层

HelloController

  1. @RestController
  2. public class HelloController {
  3. @RequestMapping("/hello")
  4. public String hello(@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime localDateTime){
  5. System.out.println("hello ==> "+localDateTime);
  6. return "success";
  7. }
  8. @RequestMapping("/hello2")
  9. public String hello2(Order order){
  10. System.out.println("hello2 ==> "+order);
  11. return "success";
  12. }
  13. @RequestMapping("/hello3")
  14. public String hello3(@RequestBody Order order){
  15. System.out.println("hello3 ==> "+order);
  16. return "success";
  17. }
  18. }

*******************

使用测试

***************

@RequestParam

localhost:8080/hello?localDateTime=2020-08-09 08:06:09

  1. hello ==> 2020-08-09T08:06:09

localhost:8080/hello2?orderId=1&orderTime=2020-08-06 08:06:09&price=2&amount=8

  1. hello2 ==> Order(orderId=1, orderTime=2020-08-06T08:06:09, price=2.0, amount=8)

@RequestParam可正常操作LocalDateTime数据

***************

@RequestBody

localhost:8080/hello3

header: Content-Type ==> application/json

body:

  1. ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzkzMTYyNQ_size_16_color_FFFFFF_t_70][]

控制台输出

  1. 2020-07-16 17:33:54.416 WARN 10404 --- [nio-8080-exec-5]
  2. .w.s.m.s.DefaultHandlerExceptionResolver : Resolved
  3. [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error:
  4. Cannot deserialize value of type `java.time.LocalDateTime` from String "2020-08-09
  5. 08:06:09": Failed to deserialize java.time.LocalDateTime:
  6. (java.time.format.DateTimeParseException) Text '2020-08-09 08:06:09' could not be parsed at
  7. index 10; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException:
  8. Cannot deserialize value of type `java.time.LocalDateTime` from String "2020-08-09
  9. 08:06:09": Failed to deserialize java.time.LocalDateTime:
  10. (java.time.format.DateTimeParseException) Text '2020-08-09 08:06:09' could not be parsed at
  11. index 10
  12. at [Source: (PushbackInputStream); line: 3, column: 18] (through reference chain:
  13. com.example.demo.pojo.Order["orderTime"])]

localDateTime反序列化失败,不能将2020-08-09 08:06:09转换为localDateTime

*******************

解决措施

***************

引入jar 包

  1. <dependency>
  2. <groupId>com.fasterxml.jackson.datatype</groupId>
  3. <artifactId>jackson-datatype-jsr310</artifactId>
  4. <version>2.11.1</version>
  5. </dependency>

***************

pojo 层

Order

  1. @Data
  2. public class Order {
  3. private String orderId;
  4. @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  5. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
  6. private LocalDateTime orderTime;
  7. private Double price;
  8. private Integer amount;
  9. }

使用@JsonFormat读取输入参数进行序列化

#

*******************

使用测试

localhost:8080/hello3

header: Content-Type ==> application/json

body:

  1. ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzkzMTYyNQ_size_16_color_FFFFFF_t_70 1][]

控制台输出

  1. hello3 ==> Order(orderId=2, orderTime=2020-08-09T08:06:09, price=2.0, amount=8)

发表评论

表情:
评论列表 (有 0 条评论,105人围观)

还没有评论,来说两句吧...

相关阅读

    相关 序列序列

    序列化: 对象的序列化主要有两种用途:   1) 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中;   2) 在网络上传送对象的字节序列。