@RequestMapping和@GetMapping区别

》 在Spring4.3版本以后,提供了@GetMapping注解更方便了开发,那么@RequestMapping和@GetMapping有什么区别呢?

1、@RequestMapping和@GetMapping区别
  • @RequestMapping可以指定GET、POST请求方式
  • @GetMapping等价于@RequestMapping的GET请求方式

使用springboot搭建springmvc测试如下:

/**
 * @auther SyntacticSugar
 * @data 2018/11/1 0001下午 7:02
 */
@RestController
@RequestMapping
public class HelloController {
    //@RequestMapping(value = "hello", method= RequestMethod.GET )
    @GetMapping("hello")
    public String hello() {
       return "hello spring boot";
    }
}

》参看@GetMapping 源码:*
源码


》springboot搭建springMVC:
步骤:
1、maven的 pom.xml中引用springboot父工程、引入启动器web-starter
2、写main函数;

    <!-- 父工程 -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <!-- 引入 starter-web -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

写 main 函数:*

/**
 * @auther SyntacticSugar
 * @data 2018/11/1 0001下午 6:56
 */
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

》controller

@RestController
@RequestMapping
public class HelloController {
    //@RequestMapping(value = "hello", method= RequestMethod.GET )
    @GetMapping("hello")
    public String hello() {
       return "hello spring boot";
    }
}

》启动main函数,浏览器发请求

请求地址:
http://localhost:8080/hello
请求结果如下:
fang

》小结

总结:
@GetMapping 在限定使用get请求时候,更为简洁方便

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值