我想在春天做一個PUT調用。既不@RequestBody也不@RequestParam工作
這是我的控制器代碼:
@RequestMapping(value = "/magic", method = RequestMethod.PUT)
TodoDTO magic(@RequestBody String id){
return service.magic(id);
}
,因爲我想通過在呼叫ID字符串。
的問題是,我收到此
{
"timestamp": 1486644310464,
"status": 500,
"error": "Internal Server Error",
"exception": "java.lang.NullPointerException",
"message": "{\n\t\"id\":\"589c5e322abb5f28631ef2cc\"\n}",
"path": "/api/todo/magic"
}
如果我改變這樣的代碼:
@RequestMapping(value = "/magic", method = RequestMethod.PUT)
TodoDTO magic(@RequestParam(value = "id") String id){
return service.magic(id);
}
我收到
{
"timestamp": 1486644539977,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MissingServletRequestParameterException",
"message": "Required String parameter 'id' is not present",
"path": "/api/todo/magic"
}
我做出同樣的呼叫,PUT在鏈接http://localhost:8080/api/todo/magic 與身體
{
"id":"589c5e322abb5f28631ef2cc"
}
這是我的數據庫中的一個對象的ID。
我的問題是,我該如何實現我的目標?如果我的鏈接,如API/TODO /魔法/ 589c5e322abb5f28631ef2cc,與@PathVariable通過帕拉姆,它的工作原理
_「with @PathVariable,It works」_然後使用它。如果你不發送數據,不要使用'PUT'。 – zeroflagL
@zeroflagL在URL中發送數據不是一種反模式嗎?這只是一個測試,這就是不做任何事情的原因:) – AvramPop
如果你的方法的要點是返回一個項目或基於某個id,那麼這個id不是真正的數據。如果ID實際上是數據,那麼你是對的,它應該作爲正文發送。但基於錯誤消息,你沒有這樣做。 – zeroflagL