Java Spring:不支持请求方法“PUT”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19967339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring: Request method 'PUT' not supported
提问by Doo Dah
I started with one of the Spring getting started samples. I am extending it to match my scenario. I am trying to use the PUT method on a web service call. I get the error message "Request method 'PUT' not supported". But, execution makes it into the web service. The error occurs after/during returning. Is there something I need to do to my objects to allow the to be returned from non-GET HTTP methods?
我从 Spring 入门示例之一开始。我正在扩展它以匹配我的场景。我正在尝试在 Web 服务调用上使用 PUT 方法。我收到错误消息“不支持请求方法‘PUT’”。但是,执行使其成为 Web 服务。返回之后/期间发生错误。我需要对我的对象做些什么来允许从非 GET HTTP 方法返回?
I am calling into the web service with a test stub written in python. I have not posted that code since execution is getting into the web service.
我正在使用用 python 编写的测试存根调用 Web 服务。由于执行正在进入 Web 服务,因此我没有发布该代码。
Following is the Spring code:
以下是Spring代码:
@ComponentScan
@EnableAutoConfiguration
@Controller
@RequestMapping("/jp5/rest/message")
public class MessageRestService
{
@RequestMapping(method=RequestMethod.PUT, value="/test")
public testResult test()
{
// I hit a breakpoint here:
return new testResult(true, "test");
}
}
class testResult
{
public testResult( boolean success, String message )
{
setSuccess(success);
setMessage(message);
}
//@XmlElement
private boolean success;
//@XmlElement
private String message;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
EditThere is no stack trace, just this in the server output:
编辑没有堆栈跟踪,在服务器输出中只有这个:
2013-11-13 21:26:20.976 WARN 5452 --- [nio-8888-exec-1]
o.s.web.servlet.PageNotFound :
Request method 'PUT' not supported
Here is the python as requested. And, I think the answer to the problem lies in "'allow': 'GET, HEAD'"in the response. So, how do I allow other methods? Maybe I need to think about an applicationContext?
这是要求的python。而且,我认为问题的答案在于响应中的“'allow': 'GET, HEAD'”。那么,我如何允许其他方法?也许我需要考虑一个 applicationContext?
path = '/jp5/rest/message/test'
method = 'PUT'
body = ''
target = urlparse(self.uri+path)
h = http.Http()
headers = {
'Accept': 'application/json',
'Content-Type': 'application/json; charset=UTF-8'
}
response, content = h.request(
target.geturl(),
method,
body,
headers)
print response
output from the print:
打印输出:
{'status': '405', 'content-length': '1045', 'content-language': 'en-US', 'server':
'Apache-Coyote/1.1', 'allow': 'GET, HEAD', 'date': 'Thu, 14 Nov 2013 02:26:20 GMT',
'content-type': 'text/html;charset=utf-8'}
I am starting the server like this:
我正在像这样启动服务器:
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Thanks
谢谢
采纳答案by Doo Dah
Thanks for the pointers. The solution is to add a @ResponseBody:
谢谢指点。解决方案是添加一个@ResponseBody:
public @ResponseBody testResult test()
{
return new testResult(true, "test");
}