有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring:不支持请求方法“PUT”

我从一个Spring入门示例开始。我正在扩展它以匹配我的场景。我正在尝试对web服务调用使用PUT方法。我收到错误消息“不支持请求方法“PUT”。但是,执行使其成为web服务。返回后/返回期间出错。是否需要对我的对象执行某些操作,以允许从非GET HTTP方法返回

我使用python编写的测试存根调用web服务。自从执行进入web服务以来,我还没有发布该代码

以下是弹簧代码:

@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。而且,我认为问题的答案在于回答中的“”允许:“'GET,HEAD'”。那么,我如何允许其他方法呢?也许我需要考虑一个应用程序上下文

    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);
    }

}

谢谢


共 (1) 个答案

  1. # 1 楼答案

    谢谢你的指点。解决方案是添加@ResponseBody:

    public @ResponseBody testResult test()
       {
           return new testResult(true, "test");
       }