1

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?

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.

Following is the Spring code:

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

Edit There 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?

    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

4

1 回答 1

5

Thanks for the pointers. The solution is to add a @ResponseBody:

public @ResponseBody testResult test()
   {
       return new testResult(true, "test");
   }
于 2013-11-15T02:02:02.213 回答