5

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>()
Community
  • 1
  • 1
Shubham A.
  • 2,446
  • 4
  • 36
  • 68

2 Answers2

7

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;
}
Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42
1

Try to add @RequestParam before your pointInTime parameter.

Kadzhaev Marat
  • 1,278
  • 1
  • 19
  • 33
  • While I get this and it is **working correctly** with `@RequestParam`. But according to OP, I want it to work as a `@PathParam`. Any ideas on that? – Shubham A. May 04 '17 at 11:16
  • `@PathParam` belongs to `javax.ws.rs` package, if you intent on using spring for developing REST web service, you should use `@RequestParam` (or any other spring annotation). Using `@PathParam` should be preffered if you use jetty. – sensitive_piece_of_horseflesh May 04 '17 at 11:32
  • @ShubhamA. ok, i'm not sure, but what if try to `@PathVariable`? – Kadzhaev Marat May 04 '17 at 11:37