2017-05-04 595 views
2

此问题与旧版Date API的this SO problem非常相似。如何接受Spring请求的GET请求中的LocalDateTime参数?

我想实现与Java 8 LocalDateTime API相同的功能。当我这样做,

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

我得到的,

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

回答

1

使用@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; 
} 
0

试图将pointInTime参数之前添加@RequestParam

+0

虽然我得到这个,这是**与'@ RequestParam'正常工作**。但根据OP,我希望它能够像'@ PathParam'一样工作。有关于此的任何想法? –

+0

'@ PathParam'属于'javax.ws.rs'包,如果您打算使用spring来开发REST Web服务,则应该使用'@ RequestParam'(或任何其他的弹簧注释)。如果您使用jetty,则应该使用'@ PathParam'。 –

+0

@ShubhamA。好的,我不确定,但如果尝试“@ PathVariable”呢? –