controller层常用注解:@RequestMapping、@PostMapping、@GetMapping总结

1. 三者的关系图 在这里插入图片描述

2. 作用

@RequestMapping、@PostMapping、@GetMapping均为映射请求路径,可作用于类或方法上。当作用于类时,是该类中所有响应请求方法地址的父路径。

3. 示例

@RequestMapping("/test") //test即父路径
public class test {

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    // @GetMapping("/hello")
    @ResponseBody // 将返回的java对象转为json格式的数据
    public String hello() {
        return "hello world !";
    }
}

访问路径:http://localhost:8080/test/hello (切记不要忘记“test”哦!)

温馨提示:读者可自行去了解POST请求和GET请求的区别
可参考:https://www.cnblogs.com/logsharing/p/8448446.html

发布了25 篇原创文章 · 获赞 1 · 访问量 440

猜你喜欢

转载自blog.csdn.net/qq_44837912/article/details/103476053