簡體   English   中英

spring 查詢參數中的啟動 offsetDateTime

[英]spring boot offsetDateTime in query param

我嘗試創建以下 api:

    @GetMapping("/workerslist")
    public List<Worker> getWorkersList(
            @RequestParam(name = "lastPollBefore", required = false) OffsetDateTime lastPollBefore,
            @RequestParam(name = "lastPollAfter", required = false) OffsetDateTime lastPollAfter){
        return workerService.getWorkers(lastPollBefore, lastPollAfter);
    }

但是當我調用它時,出現以下錯誤:

{
  "_type": "ValidationError",
  "key": "validation-error",
  "message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.OffsetDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.OffsetDateTime] for value '2020-07-01T11:52:57.152Z'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2020-07-01T11:52:57.152Z]",
  "constraints": [
    
  ]
}

我找到了 localDateTime 的解決方案,但它們似乎不適用於 offsetDateTime,

您需要為 controller 中的 OffsetDateTime arguments 添加@DateTimeFormat注釋。例如。

    @GetMapping("/workerslist")
    public List<Worker> getWorkersList(
            @RequestParam(name = "lastPollBefore", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)OffsetDateTime lastPollBefore,
            @RequestParam(name = "lastPollAfter", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)OffsetDateTime lastPollAfter){
        return workerService.getWorkers(lastPollBefore, lastPollAfter);
    }

您可能希望通過您自己的自定義映射器覆蓋 Spring 的默認 Object 映射器。

是這樣的:

@Configuration
public class JsonConfiguration {

  private static final ObjectMapper OBJECT_MAPPER =
      new ObjectMapper().registerModule(new JavaTimeModule())
          .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
          .disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
          .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

  @Bean
@Primary
  public static ObjectMapper getObjectMapper() {
    return OBJECT_MAPPER;
  }
}

我嘗試創建以下 api:

    @GetMapping("/workerslist")
    public List<Worker> getWorkersList(
            @RequestParam(name = "lastPollBefore", required = false) OffsetDateTime lastPollBefore,
            @RequestParam(name = "lastPollAfter", required = false) OffsetDateTime lastPollAfter){
        return workerService.getWorkers(lastPollBefore, lastPollAfter);
    }

但是當我調用它時,我收到以下錯誤:

{
  "_type": "ValidationError",
  "key": "validation-error",
  "message": "Failed to convert value of type 'java.lang.String' to required type 'java.time.OffsetDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.time.OffsetDateTime] for value '2020-07-01T11:52:57.152Z'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2020-07-01T11:52:57.152Z]",
  "constraints": [
    
  ]
}

我找到了 localDateTime 的解決方案,但它們似乎不適用於 offsetDateTime,

暫無
暫無

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

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