【问题标题】:GetMapping and PostMappingGetMapping 和 PostMapping
【发布时间】:2022-01-07 22:43:02
【问题描述】:
@RestController
public class HelloWorldController {

    @GetMapping(path="/helloWorld")
    public String helloWorld() {
        return "Hello-World";
    }
}

我是 RestFul WebServices 的新手。我试图用 PostMapping 注释 helloWorld() 但它失败了。使用GetMapping,成功执行。

谁能告诉我为什么不允许 PostMapping?

【问题讨论】:

    标签: spring-boot rest post get spring-restcontroller


    【解决方案1】:

    PostMapping 用于 POST 请求。

    GetMapping 用于 GET 请求。

    如果要调用 PostMapping 成功,可以使用 Postman 或 SoapUI、curl 来测试 HTTP POST 请求。

    参考文档:

    【讨论】:

    • 我们可以使用 Post 请求代替 GET 请求吗?我们可以在其他地方使用它们吗?会有什么不同吗?
    • 当然,您可以使用 GET 或 POST。但它在客户端的行为不同。例如,您需要从提交按钮的操作中触发 POST 请求。
    【解决方案2】:

    除了@Do Nhu Vys 的回答。在执行 Post 请求时,您经常会遇到 CORS 和 CRFS 问题。

    参考资料:

    https://docs.spring.io/spring-security/site/docs/5.0.x/reference/html/csrf.html https://spring.io/blog/2015/06/08/cors-support-in-spring-framework

    【讨论】:

      猜你喜欢
      • 2022-11-18
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 2020-07-30
      • 2019-04-08
      • 2018-12-14
      • 2019-11-23
      相关资源
      最近更新 更多