【发布时间】:2023-07-02 09:18:02
【问题描述】:
我从 Spring 入门示例之一开始。我正在扩展它以匹配我的场景。我正在尝试在 Web 服务调用上使用 PUT 方法。我收到错误消息“不支持请求方法‘PUT’”。但是,执行使其进入 Web 服务。错误发生在返回之后/返回期间。我需要对我的对象做些什么以允许从非 GET HTTP 方法返回?
我正在使用用 python 编写的测试存根调用 Web 服务。由于执行正在进入 Web 服务,因此我没有发布该代码。
以下是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;
}
}
编辑 没有堆栈跟踪,只是在服务器输出中:
2013-11-13 21:26:20.976 WARN 5452 --- [nio-8888-exec-1]
o.s.web.servlet.PageNotFound :
Request method 'PUT' not supported
这是所要求的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
打印输出:
{'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'}
我是这样启动服务器的:
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
谢谢
【问题讨论】:
-
我们可以看到堆栈跟踪吗?只是为了好玩 Python 代码?
-
这可能会有所帮助 [*.com/questions/14416078/… [1]: *.com/questions/14416078/…
标签: java spring spring-mvc