如何在对Spring Boot Controller的GET请求中接受LocalDateTime参数?

Shu*_* A. 5 java spring

这个问题与旧版API的SO问题非常相似Date

我想用Java 8 LocalDateTimeAPI 达到相同的目的。当我做,

@RequestMapping("/locationSnapshot/{userId}/{pointInTime}")
public MyResponse getLocationInTime(
        @PathParam(value="userId") Long userId,
        @PathParam(value="pointInTime")
        @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss") LocalDateTime pointInTime) {

    MyResponse response = new MyResponse();
    return response;
}
Run Code Online (Sandbox Code Playgroud)

我明白了

Failed to instantiate [java.time.LocalDateTime]: No default constructor
found; nested exception is java.lang.NoSuchMethodException: 
java.time.LocalDateTime.<init>()
Run Code Online (Sandbox Code Playgroud)

Mon*_*mul 5

使用@PathVariable代替@PathParam

@RequestMapping("/locationSnapshot/{userId}/{pointInTime}")
public MyResponse getLocationInTime(
        @PathVariable(value="userId") Long userId,
        @PathVariable(value="pointInTime")
        @DateTimeFormat(pattern="yyyy-MM-dd'T'HH:mm:ss") LocalDateTime pointInTime) {

    MyResponse response = new MyResponse();
    return response;
}
Run Code Online (Sandbox Code Playgroud)