没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
SpringBoot 中常用注解及各种注解作用中常用注解及各种注解作用
本篇文章将介绍几种SpringBoot 中常用注解及各个注解的作用,感兴趣的朋友跟随脚本之家小编一起学习吧
本篇文章将介绍几种SpringBoot 中常用注解
其中,各注解的作用为:
@PathVaribale 获取url中的数据
@RequestParam 获取请求参数的值
@GetMapping 组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写
@RestController是@ResponseBody和@Controller的组合注解。
@PathVaribale 获取获取url中的数据中的数据
看一个例子,如果我们需要获取Url=localhost:8080/hello/id中的id值,实现代码如下:
@RestController
public class HelloController {
@RequestMapping(value="/hello/{id}",method= RequestMethod.GET)
public String sayHello(@PathVariable("id") Integer id){
return "id:"+id;
}
}
@RequestParam 获取请求参数的值
直接看一个例子,如下
@RestController
public class HelloController {
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String sayHello(@RequestParam("id") Integer id){
return "id:"+id;
}
}
在浏览器中输入地址:localhost:8080/hello?id=1000,可以看到如下的结果:
当我们在浏览器中输入地址:localhost:8080/hello?id ,即不输入id的具体值,此时返回的结果为null。具体测试结果如下:
@GetMapping 组合注解
@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写。该注解将HTTP Get 映射到 特定的处
理方法上。
即可以使用@GetMapping(value = “/hello”)来代替@RequestMapping(value=”/hello”,method= RequestMethod.GET)。即可以让我们精简代码。
例子
@RestController
public class HelloController {
//@RequestMapping(value="/hello",method= RequestMethod.GET)
资源评论
weixin_38736011
- 粉丝: 3
- 资源: 920
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功