1. 程式人生 > >Spring boot PUT、DELETE 請求無法定位方法及獲取引數解決方案

Spring boot PUT、DELETE 請求無法定位方法及獲取引數解決方案

在使用spring boot過程中帶著之前使用spring mvc的慣性思維操作,雖然spring boot已經整合了HiddenHttpMethodFilter , 結果卻出現了 PUT方法不支援及無法獲取到引數等問題;

解決方案:

1、將”_method”併到url,這樣spring才能通過request.getParameter(this.methodParam);方法獲取到_method = put才能將其轉換為PUT請求定位至正確的控制器方法;


url:'/user?_method=PUT'

2、在引數前加註解@RequestBody , 通過json 傳遞資料:



  

$.post({
    url:'/user?_method=PUT',
    contentType:"application/json",
    dataType:'json',
    data:JSON.stringify(obj),
    success:function (data) {
        console.log(data);
    }
})