【问题标题】:Spring Controller to get both GET and POST variablesSpring Controller 获取 GET 和 POST 变量
【发布时间】:2023-01-11 13:42:51
【问题描述】:

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

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

要在 Spring 控制器类中获取 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)获取变量?

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

【问题讨论】:

    标签: java spring spring-mvc controller httprequest


    【解决方案1】:

    所以实际上,在 Get 请求的情况下,只能发送有限数量的数据,因为数据是在标头中发送的。

    在 Post 请求的情况下,可以发送大量数据,因为数据是在 body 中发送的。

    您一次只能使用一种类型的请求。尝试了解@RequestParam、@RequestBody、@PathVariable 和@ModelAttribute。

    玩得开心。附言我没有投票给你,其他人做了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-29
      • 2010-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 2015-12-16
      相关资源
      最近更新 更多