繁体   English   中英

Spring Controller 获取 GET 和 POST 变量

[英]Spring Controller to get both GET and POST variables

有时我们将带有 POST 负载的 POST HTTP 请求发送到带有 URL 变量的端点,例如:

[POST] http://example.com/update-item?itemid=123456

要在 Spring controller class 中获取 POST 负载,我可以这样做:

@RequestMapping(value = "/update-item", method = RequestMethod.POST)
public String updateItem(@RequestBody Item json) {
    //some logics
     return "/update-item-result";
}

但是,与此同时,即使对于method = RequestMethod.POST ,我如何从 URL (即上例中的itemid )获取变量?

我在 web 上看到很多 Spring MVC 示例要么从 URL 获取 GET 变量,要么从有效负载获取 POST 变量,但我从来没有看到两者都起作用。

您可以通过在 @RequestMapping 注解中将 method 属性指定为数组来使用多个 HTTP 请求。

    @RequestMapping(value = "/update-item", method = {RequestMethod.POST,RequestMethod.GET})
public String updateItem(@RequestBody Item json) {
    //some logics
     return "/update-item-result";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM