關于獲取參數的一些問題
在spring mvc 中,可以通過下面方式獲取url的?后面的,或者form中的參數,無論get請求還是post請求
? ? http://127.0.0.1:8080/userinfo?id?= 100
????public void deleteUserinfo(Integer id) {
????????System.out.println("========= id : " + id);
????}
????但是,在springboot中,卻無法通過這種方式獲取數據,必須使用?@PathVariable 或者?@RequestParam來修飾才能獲取
2. putmapping類型的請求,無法通過@RequestParam獲取參數,當在方法的參數中使用有@RequestParam注解進行參數獲取的時候,直接報400的錯誤
錯誤消息:
{
"timestamp": 1505648261494,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.web.bind.MissingServletRequestParameterException",
"message": "Required String parameter 'name' is not present",
"path": "/userinfo/userinfo3/11"
}
2017-09-17
第一個規定就是這樣,我們按照規定來就可以了。
第二個Required String parameter 'name' is not present。應該是你傳入參數是沒傳入“name”屬性,而后臺代碼你直接寫的@RequestParam("name"),這樣寫是有問題的,因為@RequestParam注解默認required = true,而你沒傳當然報錯了。可以加上required = false,這時代碼邏輯可以需要調整一下,因為不傳name時后臺獲取的name可能null。