restful風格接口寫DELETE或PUT請求時遇到405錯誤


用restful風格接口寫DELETE或PUT請求時遇到405錯誤

Jsp上有個PUT請求的表單:

<form action="springmvc/testRestFulPut/1" method="post">
<input type="hidden" value="put" name="_method">
<input type="submit" value="PUT Submit">
</form>

SpringMVC的接口函數:

// 更新
@RequestMapping(value="/testRestFulPut/{id}",method=RequestMethod.PUT)
public String testRestFulPUT(@PathVariable("id") Integer id) {
System.out.println("test PUT : " + id);
return SUCCESS;
}

web.xml配置

<!-- 為了讓普通瀏覽器表單能發送put或delete請求 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

 

當在瀏覽器訪問這個接口時會產生405錯誤,而控制台輸出正常:

瀏覽器 
瀏覽器

控制台

 

 

解決辦法: 
1.加入 @ResponseBody 注解。 
2.請求先轉給一個Controller,再返回jsp頁面。 
3.tomcat換到7.0以及以下版本。

4.加入isErrorPage=“true”

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM