JavaEE进阶知识学习-----SpringBoot基础知识-3-控制类知识

下面将简单介绍一下controller类的相关用法

在上一篇博客中,我们也使用的Controller相关的注解,下面我们来简单的总结一下:
1. @RestController处理Http请求,返回JSON格式的数据。
2. @RequestMapping(value = “/hello”,method = RequestMethod.GET)配置URL映射

如果使用多个URL访问同一个的方法,可以将URL映射配置为一个集合,如下所示:

@RestController
public class HelloSpringBoot {
    @Autowired
    private UserProperties userProperties;
    @RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
    public String hello(){
        return userProperties.getCupSize();
    }
}

RequestMapping类注解如下:

@RestController
@RequestMapping("demo")
public class HelloSpringBoot {
    @Autowired
    private UserProperties userProperties;
    @RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
    public String hello(){
        return userProperties.getCupSize();
    }
}

访问链接为http://localhost:8082/gire/demo/hello

如何获取参数

  1. @PathVariable获取URL中的数据
  2. @RequestParam获取请求参数的值
  3. @GetMapping组合注解
@PathVariable注解使用
@RestController
@RequestMapping("demo")
public class HelloSpringBoot {
    @Autowired
    private UserProperties userProperties;
    @RequestMapping( value = "/hello/{id}",method = RequestMethod.GET)
    public String hello(@PathVariable("id") Integer id){
        return "id:"+id;
    }
}

访问链接如下:http://localhost:8082/gire/demo/hello/5

@RequestParam注解使用

如果使用传统的传参数http://localhost:8082/gire/demo/hello?id=5那么获取方式如下所示:

@RestController
@RequestMapping("demo")
public class HelloSpringBoot {
    @Autowired
    private UserProperties userProperties;
    @RequestMapping( value = "/hello",method = RequestMethod.GET)
    public String hello(@RequestParam("id") Integer id){
        return "id:"+id;
    }
}

也可以使用默认参数值,和要求是否必传,如下所示:

@RestController
@RequestMapping("demo")
public class HelloSpringBoot {
    @Autowired
    private UserProperties userProperties;
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    public String hello(@RequestParam(value = "id",required = false,defaultValue = "0") Integer id){
        return "id:"+id;
    }
}

其中required要求是否必传,defaultValue是默认值,如果不传id则显示默认值。

组合注解

@RequestMapping(value = “/hello”,method = RequestMethod.GET)这个注解可以使用 @GetMapping(value = “/hello”)这个组合注解来替代,当然 @PutMapping、 @DeleteMapping等形式。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值