V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cencoroll
V2EX  ›  Java

@DateTimeFormat 转换报错

  •  
  •   cencoroll · 239 天前 · 1238 次点击
    这是一个创建于 239 天前的主题,其中的信息可能已经有所发展或是发生改变。

    今天碰到一个情况就是 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 条回复    2023-09-01 17:19:57 +08:00
    xianyv
        1
    xianyv  
       239 天前
    为啥你的 DateTimeFormat 的包名是 com.alibaba 的?不是 org.springframework.format.annotation 的吗
    missz
        2
    missz  
       239 天前
    2023-9-01 换成 2023-09-01 试试
    cencoroll
        3
    cencoroll  
    OP
       239 天前
    @missz 都试过了,2023-9-1 2023-09-01 2023-9-01 这三个一样都是报错
    @xianyv 这两个报名一样报错,都试过了,排查了一上午了
    cencoroll
        4
    cencoroll  
    OP
       239 天前
    @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  
       239 天前
    用 LocalDate
    oldshensheep
        6
    oldshensheep  
       239 天前
    2023 年了还有人在用 java.util.Date
    换成 java.time.Instant
    Huelse
        7
    Huelse  
       239 天前
    java.time.LocalDate
    wengyanbin
        8
    wengyanbin  
       239 天前
    本地跑了下,没有报错。
    @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());
    }
    }
    cencoroll
        9
    cencoroll  
    OP
       239 天前
    @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  
       239 天前
    @cencoroll 这个看着应该是参数转化器的问题。接受参数用的是哪个注解?
    jamezee
        11
    jamezee  
       239 天前
    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  
       239 天前
    前端传的参数是什么样的
    wengyanbin
        13
    wengyanbin  
       239 天前
    @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  
       239 天前
    把你 fastjson 换成 jackson 试试,为啥改默认的
    cencoroll
        15
    cencoroll  
    OP
       238 天前
    @hrapunzel 9 楼那贴出来了,@GetMapping 用 url 传的参数。list?queryType=TEAM&date=2023-9-01 (这里的不管是 09-01 还是 9-1 样都是报错)
    @cslive fastjson 和 jackson 两个都试过,一样都是报错
    xianyv
        16
    xianyv  
       238 天前
    切面或者拦截器看一下自己到底接收了什么东西
    kraken9527
        17
    kraken9527  
       238 天前 via Android
    看看 class 文件是这个内容嘛,实在不行加个断点 debug 一下
    Naccl
        18
    Naccl  
       238 天前
    看看项目配置文件里,有没有这类冲突的配置
    spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
    Naccl
        19
    Naccl  
       238 天前
    或者看下有没有注入过 Date Converter 的 bean
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2884 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 07:52 · PVG 15:52 · LAX 00:52 · JFK 03:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.