同一方法的@PutMapping和@PostMapping注释

Raj*_*Raj 5 java spring spring-mvc spring-boot

我想将Put和Post映射请求应用于如下所示的方法.它适用于PUT,但不适用于POST请求.我错了什么?

@RestController
@RequestMapping("/PQR")
public class XController {

    @PutMapping("xyz")
    @PostMapping("xyz")
    public MyDomainObject createOrUpdateDAO(
            HttpServletRequest request, 
            @RequestBody String body) throws IOException {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

当我发出POST请求时,我得到一个405 HTTP状态代码:

[nio-8080-exec-3] osweb.servlet.PageNotFound:不支持请求方法'POST'

如果我看一下这个例子,同样的方法有相同的方法被映射为GET和POST请求.

@RequestMapping(value="/method3", method = { RequestMethod.POST,RequestMethod.GET })
@ResponseBody
public String method3() {
    return "method3";
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*win 6

删除@PostMapping@PutMapping注释并添加method到您的@RequestMapping,即:

@RequestMapping(value={"/PQR", "xyz"},
    method={RequestMethod.POST,RequestMethod.PUT})
Run Code Online (Sandbox Code Playgroud)