【Java】解決POST表單提交報錯 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

1.報錯場景

前端POST表單提交,後臺服務報錯 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

 

2.解決方法

application/x-www-form-urlencoded;charset=UTF-8 是以鍵值對拼接的形式,如 name=abc&phone=123456,並不是application/json那樣的json格式{"name":"abc","phone":"123456"}

所以做以下修改:

原代碼示例:

@PostMapping("save")
public Result save(@RequestBody User user) {

}

修改後的代碼示例:

@PostMapping("save")
public Result save(@RequestParam Map<String, Object> params) {

}

將原本的接收形式@RequestBody改成@RequestParam,同時用Map來接收參數。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章