简体   繁体   English

Spring Controller 获取 GET 和 POST 变量

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

Sometimes we send a POST HTTP request with POST payload to an endpoint with URL variable, for example:有时我们将带有 POST 负载的 POST HTTP 请求发送到带有 URL 变量的端点,例如:

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

To get the POST payload in the Spring controller class, I can do something this:要在 Spring controller class 中获取 POST 负载,我可以这样做:

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

However, at the same time, how can I get the variable from the URL (ie itemid in the above example) even for method = RequestMethod.POST ?但是,与此同时,即使对于method = RequestMethod.POST ,我如何从 URL (即上例中的itemid )获取变量?

I see a lot of Spring MVC examples on the web either get the GET variables from the URL or the POST variables from the payload, but I never see getting both in action.我在 web 上看到很多 Spring MVC 示例要么从 URL 获取 GET 变量,要么从有效负载获取 POST 变量,但我从来没有看到两者都起作用。

You can use multiple HTTP requests by specifying the method attribute as an array in the @RequestMapping annotation.您可以通过在 @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