簡體   English   中英

無法將字符串 @RequestParam 轉換為 javasql.Timestamp

[英]Fail to convert String @RequestParam to javasql.Timestamp

我有一個單元測試來檢查具有 TimeStamp 參數的獲取請求。

UT 將當前 TimeStemp 的請求模擬為 String。

Timestamp myTimeStamp = new Timestamp(Calendar.getInstance().getTimeInMillis());

MockHttpServletRequestBuilder request =get(REST_PATH).param("horarioFim", mytimeStamp.toString());

在我的 controller 我有

    @RequestMapping(method = GET)
    public Iterable<AuditVersionModel> search(@RequestParam(required = false) Timestamp horarioFim)

時間戳從 java.sql.TimeStamp 導入

測試失敗,我有這個信息。

MockHttpServletResponse:
           Status = 500
    Error message = null
          Headers = {Content-Type=[application/json;charset=UTF-8]}
     Content type = application/json;charset=UTF-8
             Body = {"timestamp":"2020-05-07T23:58:48.298+0000","status":999,"error":"None","message":"Failed to convert value of type 'java.lang.String' to required type 'java.sql.Timestamp'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.sql.Timestamp] for value '2020-05-07 20:58:48.201'; nested exception is java.lang.IllegalArgumentException: Invalid format: \"2020-05-07 20:58:48.201\" is malformed at \" 20:58:48.201\""}
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

我嘗試使用@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)和其他變體但沒有成功,它總是同樣的錯誤。

OBS1:本項目是從Spring Boot 1.5遷移到2.0,代碼簡化。 OBS2:這是一個遺留系統,測試是目前驗證請求的唯一方法。

假設您使用的反序列化框架期望日期格式為 ISO 格式,則傳入的日期不是 ISO 格式。 我嘗試將“2020-05-07T23:58:48.298+0000”傳遞給OffsetDateTime.parse() API 並引發異常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM