@DateTimeFormat 转换报错 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
KamL
V2EX    Java

@DateTimeFormat 转换报错

  •  
  •   KamL 2023-09-01 13:12:21 +08:00 2117 次点击
    这是一个创建于 773 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    报错信息是这样的

    code:400 data:{} msg: "date:Failed t 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 条回复    2023-09-01 17:19:57 +08:00
    xianyv
        1
    xianyv  
       2023-09-01 13:17:49 +08:00
    为啥你的 DateTimeFormat 的包名是 com.alibaba 的?不是 org.springframework.format.annotation 的吗
    missz
        2
    missz  
       2023-09-01 13:19:24 +08:00
    2023-9-01 换成 2023-09-01 试试
    KamL
        3
    KamL  
    OP
       2023-09-01 13:24:02 +08:00
    @missz 都试过了,2023-9-1 2023-09-01 2023-9-01 这三个一样都是报错
    @xianyv 这两个报名一样报错,都试过了,排查了一上午了
    KamL
        4
    KamL  
    OP
       2023-09-01 13:26:44 +08:00
    @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
        5
    TinyBad  
       2023-09-01 13:37:04 +08:00
    用 LocalDate
    oldshensheep
        6
    oldshensheep  
       2023-09-01 13:41:26 +08:00
    2023 了还有人在用 java.util.Date
    换成 java.time.Instant
    Huelse
        7
    Huelse  
       2023-09-01 13:51:35 +08:00
    java.time.LocalDate
    wengyanbin
        8
    wengyanbin  
       2023-09-01 14:08:07 +08:00
    本地跑了下,没有报错。
    @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
        9
    KamL  
    OP
       2023-09-01 14:13:13 +08:00
    @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
        10
    wengyanbin  
       2023-09-01 14:50:58 +08:00
    @cencoroll 这个看着应该是参数转化器的问题。接受参数用的是哪个注解?
    jamezee
        11
    jamezee  
       2023-09-01 14:57:05 +08:00
    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
        12
    hrapunzel  
       2023-09-01 15:00:27 +08:00
    前端传的参数是什么样的
    wengyanbin
        13
    wengyanbin  
       2023-09-01 15:09:22 +08:00
    @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
        14
    cslive  
       2023-09-01 15:18:51 +08:00
    把你 fastjson 换成 jackson 试试,为啥改默认的
    KamL
        15
    KamL  
    OP
       2023-09-01 16:15:12 +08:00
    @hrapunzel 9 楼那贴出来了,@GetMapping 用 url 传的参数。list?queryType=TEAM&date=2023-9-01 (这里的不管是 09-01 还是 9-1 样都是报错)
    @cslive fastjson 和 jackson 两个都试过,一样都是报错
    xianyv
        16
    xianyv  
       2023-09-01 16:48:41 +08:00
    切面或者拦截器看一下自己到底接收了什么东西
    kraken9527
        17
    kraken9527  
       2023-09-01 16:53:44 +08:00 via Android
    看看 class 文件是这个内容嘛,实在不行加个断点 debug 一下
    Naccl
        18
    Naccl  
       2023-09-01 17:15:20 +08:00
    看看项目配置文件里,有没有这类冲突的配置
    spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
    Naccl
        19
    Naccl  
       2023-09-01 17:19:57 +08:00
    或者看下有没有注入过 Date Converter 的 bean
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5026 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 03:59 PVG 11:59 LAX 20:59 JFK 23:59
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86