Spring Boot 学习之路(2)
版本:java 1.8
maven 3.5.4
springboot 2.0.5

Controller中一些注解的使用

Controller上使用的注解
The Road Of Study
1、@Controller注解要配合模板进行使用
在pom.xml增加dependency节点
The Road Of Study
然后在resources文件夹里,增加一个template文件目录
里面新建一个index.html文件,
在Controller类里,return“index”

2、RestController相当于@[email protected] 组合起来的效果

3、RequestMapping
配置在类上时
@RequestMapping("/hello")配置访问的路径
配置在方法上时
@RequestMapping(value = “/say” , method = RequestMethod.GET) 配置访问方法的路径
@RequestMapping(value = {"/hello","/hi"} , method = RequestMethod.GET) 配置多个路径

The Road Of Study

1、PathVariable用法:
@GetMapping(value = “/say/{id}”) ({id}也可以在say之前,"/{id}/say")
public String say(@PathVariable(“id”) Integer id)
访问地址:http://127.0.0.1:8080/hello/say/666

2、RequestParam用法:
第一种:必须带参数的
@RequestMapping(value = “/say” , method = RequestMethod.GET)
public String say(@RequestParam(“id”) Integer myId)
访问地址:http://127.0.0.1:8080/hello/say?id=111
第二种:可以设置默认参数的
public String say(@RequestParam(value = “id” ,required = false , defaultValue = “0”) Integer myId)
当访问时没有带参数,默认为0

3、GetMapping用法
相当于@RequestMapping+method = RequestMethod.GET的组合
PostMapping等等同理

相关文章: