如何在 Spring 中使用 LocalDateTime RequestParam?我收到“无法将字符串转换为 LocalDateTime”

新手上路,请多包涵

我使用 Spring Boot 并将 jackson-datatype-jsr310 包含在 Maven 中:

 <dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.7.3</version>
</dependency>

当我尝试使用具有 Java 8 日期/时间类型的 RequestParam 时,

 @GetMapping("/test")
public Page<User> get(
    @RequestParam(value = "start", required = false)
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime start) {
//...
}

并使用此 URL 对其进行测试:

 /test?start=2016-10-8T00:00

我收到以下错误:

 {
  "timestamp": 1477528408379,
  "status": 400,
  "error": "Bad Request",
  "exception": "org.springframework.web.method.annotation.MethodArgumentTypeMismatchException",
  "message": "Failed to convert value of type [java.lang.String] to required type [java.time.LocalDateTime]; 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.time.LocalDateTime] for value '2016-10-8T00:00'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2016-10-8T00:00]",
  "path": "/test"
}

原文由 Kery Hu 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
2 个回答

TL;DR - 您可以仅使用 @RequestParam 将其捕获为字符串,或者您也可以让 Spring 通过 @DateTimeFormat 在参数上将字符串另外解析为 java 日期/时间类.

@RequestParam 足以获取您在 = 符号后提供的日期,但是,它作为 String 进入方法。这就是它抛出强制转换异常的原因。

有几种方法可以实现这一点:

  1. 自己解析日期,将值作为字符串获取。
 @GetMapping("/test")
public Page<User> get(@RequestParam(value="start", required = false) String start){

    //Create a DateTimeFormatter with your required format:
    DateTimeFormatter dateTimeFormat =
            new DateTimeFormatter(DateTimeFormatter.BASIC_ISO_DATE);

    //Next parse the date from the @RequestParam, specifying the TO type as
a TemporalQuery:
   LocalDateTime date = dateTimeFormat.parse(start, LocalDateTime::from);

    //Do the rest of your code...
}

  1. 利用 Spring 自动解析和期望日期格式的能力:
 @GetMapping("/test")
public void processDateTime(@RequestParam("start")
                            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
                            LocalDateTime date) {
        // The rest of your code (Spring already parsed the date).
}

原文由 Bwvolleyball 发布,翻译遵循 CC BY-SA 4.0 许可协议

你做的一切都是正确的:)。 是一个示例,可以准确显示您在做什么。 只需@DateTimeFormat 注释您的 RequestParam。无需在控制器中进行特殊 GenericConversionService 或手动转换。 This blog post写了它。

 @RestController
@RequestMapping("/api/datetime/")
final class DateTimeController {

    @RequestMapping(value = "datetime", method = RequestMethod.POST)
    public void processDateTime(@RequestParam("datetime")
                                @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime dateAndTime) {
        //Do stuff
    }
}

我猜你的格式有问题。在我的设置中,一切正常。

原文由 d0x 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题