0

You can only use GET and POST for xDomainRequests. Is there a way I can create a mapping that can also take an optional param to determine which webmethod to use? For example I have:

@RequestMapping(method = RequestMethod.PUT)
@ResponseBody
public SomeObject someUpdateFunction(@RequestBody SomeObject objectToUpdate)
{
    ...
}

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public SomeObject someCreateFunction(@RequestBody SomeObject objectToUpdate)
{
    ...
}

Is there a way that I can somehow map an XDR to the PUT method? I obviously don't want to add the POST option to someUpdateFunction().

4

1 回答 1

1

O'Reilly 的书“RESTful Web Services”描述了一种约定,其中预期的方法通过查询字符串或请求正文包含在“_method”参数中。当您在 IE9 和更早版本中处理跨域 ajax 请求时,这特别有用,其中只允许 GET 和 POST。在这种情况下,您将包含一个值为“PUT”的 _method 参数。

所以,如果你遵循我刚才描述的约定,也许你可以做这样的事情:

private SomeObject processPut(objectToUpdate) 
{
    ...
}

@RequestMapping(method = RequestMethod.PUT)
@ResponseBody
public SomeObject someUpdateFunction(@RequestBody SomeObject objectToUpdate)
{
    return processPut(objectToUpdate);
}

@RequestMapping(method = RequestMethod.POST, params = "_method=PUT")
@ResponseBody
public SomeObject someUpdateFunction(@RequestBody SomeObject objectToUpdate)
{
    return processPut(objectToUpdate);
}

@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public SomeObject someCreateFunction(@RequestBody SomeObject objectToUpdate)
{
    ...
}

也许在 Spring 中有更好的方法来做到这一点,因为我对这个框架的经验很少,但这个概念应该成立。

于 2013-09-25T16:25:05.997 回答