KamL
V2EX  ›  Java

@DateTimeFormat 转换报错

  •  
  •   KamL · Sep 1, 2023 · 3538 views
    This topic created in 1166 days ago, the information mentioned may be changed or developed.

    今天碰到一个情况就是 get 请求到后端的时候日期无法转换,但是 Date 上明明使用了注解但是就是报错

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    //@JsonFormat(pattern = "yyyy-MM-dd" , timezone="GMT+8")
    private Date date;
    

    报错信息是这样的

    code:400
    data:{}
    msg: 
    "date:Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.alibaba.excel.annotation.format.DateTimeFormat java.util.Date] for value '2023-9-01'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-9-01]"
    success:false
    

    迷惑的点在于如果加上 HH:mm:ss 的话,把日期+00:00:00 就不会报错。 请教一下大佬到底怎么办才能让后端正确接收这个不带时分秒的日期?

    19 replies  •  2023-09-01 17:19:57 +08:00
    xianyv
    xianyv
    Sep 1, 2023
    1
    为啥你的 DateTimeFormat 的包名是 com.alibaba 的?不是 org.springframework.format.annotation 的吗
    missz
    missz
    Sep 1, 2023
    2
    2023-9-01 换成 2023-09-01 试试
    KamL
    KamL
    OP
    Sep 1, 2023
    3
    @missz 都试过了,2023-9-1 2023-09-01 2023-9-01 这三个一样都是报错
    @xianyv 这两个报名一样报错,都试过了,排查了一上午了
    KamL
    KamL
    OP
    Sep 1, 2023
    4
    @cencoroll 报名-->包名,基本把能排查的都排查了一遍了,就是说转换异常

    ```
    date:Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@com.fasterxml.jackson.annotation.JsonFormat @org.springframework.format.annotation.DateTimeFormat java.util.Date] for value '2023-09-01'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2023-09-01]
    ```
    TinyBad
    TinyBad
    Sep 1, 2023
    5
    用 LocalDate
    oldshensheep
    oldshensheep
    Sep 1, 2023
    6
    2023 年了还有人在用 java.util.Date
    换成 java.time.Instant
    Huelse
    Huelse
    Sep 1, 2023
    7
    java.time.LocalDate
    wengyanbin
    wengyanbin
    Sep 1, 2023
    8
    本地跑了下,没有报错。
    @Getter
    @Setter
    public class UserModel {

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    //@JsonFormat(pattern = "yyyy-MM-dd")
    private Date birthday;

    public static void main(String[] args) throws JsonProcessingException {
    String a="{\"birthday\":\"2010-01-01\"}";
    ObjectMapper objectMapper=new ObjectMapper();
    UserModel user = objectMapper.readValue(a, UserModel.class);
    System.out.println(user.getBirthday());
    }
    }
    KamL
    KamL
    OP
    Sep 1, 2023
    9
    @wengyanbin 头都大了,如果带时分秒就能转换成功。但是不带的话一定报错
    ```
    list?queryType=TEAM&date=2023-9-01+00:00:00

    ```
    像这样就完全没问题
    ```
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd" , timezone="GMT+8")
    private Date date;
    ```
    wengyanbin
    wengyanbin
    Sep 1, 2023
    10
    @cencoroll 这个看着应该是参数转化器的问题。接受参数用的是哪个注解?
    jamezee
    jamezee
    Sep 1, 2023
    11
    import com.fasterxml.jackson.annotation.JsonFormat;
    import org.springframework.format.annotation.DateTimeFormat;
    ...

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd" , timezone="GMT+8")
    private Date date;

    我这的项目里就这么用的,没问题,可能是版本或者配置的问题
    hrapunzel
    hrapunzel
    Sep 1, 2023
    12
    前端传的参数是什么样的
    wengyanbin
    wengyanbin
    Sep 1, 2023
    13
    @cencoroll
    @RestController
    @RequestMapping("/test")
    public class TestController {

    @GetMapping("/list")
    public ResponseEntity<String> list(UserModel userModel){
    System.out.println(userModel.getBirthday());
    return ResponseEntity.ok("success");
    }
    }
    我在 controller 里面这么用也是可以的,localhost:8082/test/list?birthday=2010-08-01 ,这是请求的路径,可以转化没有报错。
    cslive
    cslive
    Sep 1, 2023
    14
    把你 fastjson 换成 jackson 试试,为啥改默认的
    KamL
    KamL
    OP
    Sep 1, 2023
    15
    @hrapunzel 9 楼那贴出来了,@GetMapping 用 url 传的参数。list?queryType=TEAM&date=2023-9-01 (这里的不管是 09-01 还是 9-1 样都是报错)
    @cslive fastjson 和 jackson 两个都试过,一样都是报错
    xianyv
    xianyv
    Sep 1, 2023
    16
    切面或者拦截器看一下自己到底接收了什么东西
    kraken9527
    kraken9527
    Sep 1, 2023 via Android
    17
    看看 class 文件是这个内容嘛,实在不行加个断点 debug 一下
    Naccl
    Naccl
    Sep 1, 2023
    18
    看看项目配置文件里,有没有这类冲突的配置
    spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
    Naccl
    Naccl
    Sep 1, 2023
    19
    或者看下有没有注入过 Date Converter 的 bean
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3516 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 64ms · UTC 04:07 · PVG 12:07 · LAX 20:07 · JFK 23:07
    ♥ Do have faith in what you're doing.