DELETE请求方法的Spring REST [英] Spring REST for DELETE Request Method
问题描述
在我的控制器中有以下方法:
@RequestMapping(value =processPurchase / {poid},method = RequestMethod.DELETE)
public String processOrder(@PathVariable int poid){
//做一些处理
return acceptPurchaseForm;
}
我的HTML
< form id =purchase-list-formclass =form-horizontalaction =/ MyNewApp / processPurchase /method =post>
< input type =hiddenname =_ methodvalue =delete>
< input type =hiddenname =poidvalue =>
以上我仍然收到以下错误:
WARN:org.springframework.web.servlet.PageNotFound - 请求方法'DELETE'不受支持
首先,我假设你有 .org / spring / docs / current / javadoc-api / org / springframework / web / filter / HiddenHttpMethodFilter.htmlrel =nofollow> HiddenHttpMethodFilter 在web.xml中配置。它需要将您的 _method
的值为delete,并将其转换为DELETE RequestMethod
poid
被传递到请求的主体中,但在你的控制器中,你期望它被传递到URL本身。这可能解释了为什么Spring无法映射请求。 编辑1:
传递 poid
在URL中,当你的HTML生成时,你必须包含在你的表单动作中。这取决于你的视图技术(我使用Freemarker),但你需要做这样的事情:
< form action =/ MyNewApp / processPurchase / $ {poid}method =post>
假设poid被写入绑定到您视图的模型中。
I have the following method in my controller
@RequestMapping(value = "processPurchase/{poid}", method = RequestMethod.DELETE)
public String processOrder(@PathVariable int poid) {
// do some processing
return acceptPurchaseForm;
}
My HTML
<form id="purchase-list-form" class="form-horizontal" action="/MyNewApp/processPurchase/" method="post">
<input type="hidden" name="_method" value="delete">
<input type="hidden" name="poid" value="">
With the above I still get the following error
WARN : org.springframework.web.servlet.PageNotFound - Request method 'DELETE' not supported
Any help appreciated.
First of all, I assume you have the HiddenHttpMethodFilter configured in your web.xml. It is required to convert your _method
with value delete to the DELETE RequestMethod
Secondly, the poid
is being passed in the body of the request but in your controller, you are expecting it to be passed in the URL itself. This might explain why Spring is unable to map the request.
EDIT 1:
To pass poid
in URL, you will have to include in your form action when your HTML is generated. It depends on your view technology (I use Freemarker) but you would be required to do something like this:
<form action="/MyNewApp/processPurchase/${poid}" method="post">
Assuming that the poid is written to the model that is binded to your view.
这篇关于DELETE请求方法的Spring REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!