》 在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
请求结果如下:
》小结
总结:
@GetMapping 在限定使用get请求时候,更为简洁方便