Spring Boot注解笔记

此篇笔记主要解释了我的另一篇文章里使用过的注解:Spring Boot之连接数据库(Jpa)+增删改查的实现

1.@RestController

  • @RestController注解相当于@ResponseBody和@Controller的结合
  • @RestController注解时,返回对象会被自动序列化成JSON
  • @RestController注解时,返回的是内容实例

ps:
@ResponseBody:

The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.

意思是:@ResponseBody告诉控制器返回对象会被自动序列化成JSON,并且传回HttpResponse这个对象

2.@Autowired

  • 它可以对类成员变量、方法及构造函数进行标注,让 spring 完成 bean 自动装配的工作。
  • 深入解读:https://juejin.im/entry/5ad3fda5f265da238d512a98

3.@PutMapping/@GetMapping等

Spring4.3中引进了{@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping}来帮助简化常用的HTTP方法的映射 并更好地表达被注解方法的语义

可以用来处理http的各种请求,后面带参数的格式见图2
例子:
在这里插入图片描述
在这里插入图片描述

4.@PathVariable/@RequestParam

  • @PathVariable是用来获得请求url中的动态参数的
  • @RequestParam将请求参数绑定到你控制器的方法参数上

在这里插入图片描述
在这里插入图片描述关于@RequestParam:

它有三个属性:value,required,defaultValue
格式:@RequestParam(value=”参数名”, required=true/false, defaultValue=””)
作用:

  1. value:请求参数名,必须配置
  2. required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;
  3. defaultValue:默认值,表示如果请求中没有同名参数时的默认值
发布了17 篇原创文章 · 获赞 1 · 访问量 1011

猜你喜欢

转载自blog.csdn.net/weixin_44822335/article/details/105429431