-
Notifications
You must be signed in to change notification settings - Fork 131
3 springboot controller shi yong
runzhenghengbin edited this page Jan 27, 2019
·
1 revision
针对controller 中 如何使用注解进行解析
- 返回数据类型为 Json 字符串,特别适合我们给其他系统提供接口时使用。
(1) 不同前缀访问同一个方法,此时访问hello和hi 都可以访问到say()这个方法
@RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
public String say(){
return girlProperties.getName();
}
(2)给类一个RequestMapping, 访问时就是:http://localhost:8099/hello/say
@RestController
@RequestMapping("/hello")
public class HelloController {
@Resource
private GirlProperties girlProperties;
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(){
return girlProperties.getName();
}
}
@RestController
@RequestMapping("/hello")
public class HelloController {
@Resource
private GirlProperties girlProperties;
@RequestMapping(value = "/say/{id}",method = RequestMethod.GET)
public String say(@PathVariable("id") Integer id){
return "id :"+id;
}
}
访问http://localhost:8099/hello/say/100, 结果如下
id :100
(1) 正常请求
@RestController
@RequestMapping("/hello")
public class HelloController {
@Resource
private GirlProperties girlProperties;
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(@RequestParam("id") Integer id){
return "id :"+id;
}
}
访问 http://localhost:8099/hello/say?id=111 结果如下
id :111
(2)设置参数非必须的,并且设置上默认值
@RestController
@RequestMapping("/hello")
public class HelloController {
@Resource
private GirlProperties girlProperties;
@RequestMapping(value = "/say",method = RequestMethod.GET)
public String say(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){
return "id :"+id;
}
}
访问http://localhost:8099/hello/say 结果如下
id :0
- 这里对应的就是下面这句代码
@GetMapping("/say")
//等同于下面代码
@RequestMapping(value = "/say",method = RequestMethod.GET)
对一个搞技术的人来说,年龄的增长其实不是那么可怕,可怕的是你没有匹配自己年龄的技术深度。
学习 spring boot 的个人笔记。
- SpringBoot(一)_快速实战搭建项目
- SpringBoot(二)_项目属性配置
- SpringBoot(三)_controller的使用
- SpringBoot(四)_SpringDataJPA的使用
- SpringBoot(五)_表单验证
- SpringBoot(六)_AOP统一处理请求
- SpringBoot(七)_统一异常处理
- SpringBoot(八)_springboot集成swagger2
- SpringBoot(九)_springboot集成MyBatis
- SpringBoot(十)_springboot集成Redis
- SpringBoot(十一)_springboot导入excel读取excel中的数据
- SpringBoot(十二)_springboot整合RabbitMQ
- SpringBoot(十三)_springboot实现预览pdf
- SpringBoot(十四)_springboot整合JasperReport6.6.0
- SpringBoot(十五)_springboot跨域处理