2017-05-04 174 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」呢? –