【问题标题】:When did you use @RequestBody and @Pathvariable and @RequestParam你什么时候使用@RequestBody 和@Pathvariable 和@RequestParam
【发布时间】:2020-10-04 08:08:44
【问题描述】:

我有一些在 Spring Boot 中使用微服务架构的应用程序。我在 RestTemplate 中使用 @PathVariable 发送了查询参数、对象、模型等。开发应用程序后,我做了一些研究,并要求使用@RequestParam 和@RequestBody。但我无法理解,也不知道如何使用@RequestBody 和@RequestParam。使用 @RequestBody 代替 @PathVariable 有什么好处。

我已经使用 Spring boot RestTemplate 发送了 GET 请求。

【问题讨论】:

    标签: spring-boot rest servlets resttemplate


    【解决方案1】:

    简而言之,Path 变量和请求参数将成为 URL 的一部分,请求正文将成为请求正文的一部分。

    当您想定位资源时,请使用 @PathVariable,即通过 id 获取/删除用户。

    /users/{id}
    

    当您想要查询/搜索/过滤数据时,请使用查询参数。

    users?lastName=abc
    

    当您想保存用户时,请查看请求正文。

    {
    "username" : "abc",
    "email" : "abc@gmail.com"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-13
      • 2014-08-16
      • 2015-03-18
      • 2013-10-28
      • 2015-08-28
      • 2018-05-05
      • 1970-01-01
      相关资源
      最近更新 更多