Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to accept LocalDateTime param in a GET request to Spring Boot Controller?

Tags:

java

spring

This question is very similar to this SO problem which is for older Date API.

I want to achieve the same with Java 8 LocalDateTime API. When I do,

@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;
}

I get,

Failed to instantiate [java.time.LocalDateTime]: No default constructor
found; nested exception is java.lang.NoSuchMethodException: 
java.time.LocalDateTime.<init>()
like image 397
Shubham A. Avatar asked May 04 '17 10:05

Shubham A.


2 Answers

Use @PathVariable instead of @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;
}
like image 79
Monzurul Shimul Avatar answered Nov 04 '22 11:11

Monzurul Shimul


Try to add @RequestParam before your pointInTime parameter.

like image 26
Kadzhaev Marat Avatar answered Nov 04 '22 11:11

Kadzhaev Marat