【问题标题】:@PutMapping and @PostMapping annotations on same method同一方法上的 @PutMapping 和 @PostMapping 注释
【发布时间】:2018-06-06 11:54:31
【问题描述】:

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

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

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

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

[nio-8080-exec-3] o.s.web.servlet.PageNotFound:不支持请求方法“POST”

如果我查看this example,相同的方法具有相同的方法映射到 GET 和 POST 请求。

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

【问题讨论】:

  • 如果您已经拥有RequestMapping,那么PostMappingPutMapping 就没有必要了。

标签: java spring spring-mvc spring-boot


【解决方案1】:

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

@RequestMapping(value={"/PQR", "xyz"},
    method={RequestMethod.POST,RequestMethod.PUT})

【讨论】:

    猜你喜欢
    • 2018-12-14
    • 2020-02-16
    • 2020-07-30
    • 2016-06-04
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    相关资源
    最近更新 更多