Wondercease

浙ICP备2022017321号

springboot 请求相关注解

5 种常见的请求类型:

  • GET:请求从服务器获取特定资源。举个例子:GET /users(获取所有学生)
  • POST:在服务器上创建一个新的资源。举个例子:POST /users(创建学生)
  • PUT:更新服务器上的资源(客户端提供更新后的整个资源)。举个例子:PUT /users/12(更新编号为 12 的学生)
  • DELETE:从服务器删除特定的资源。举个例子:DELETE /users/12(删除编号为 12 的学生)
  • PATCH:更新服务器上的资源(客户端提供更改的属性,可以看做作是部分更新)

@GetMapping(“users”)` 等价于`@RequestMapping(value=”/users”,method=RequestMethod.GET)

PATCH 请求

PUT 不够用了之后才用 PATCH 请求去更新数据

@PatchMapping(“/profile”)

传值

@PathVariable 和 @RequestParam

@PathVariable用于获取路径参数,@RequestParam用于获取查询参数。

@GetMapping(“/klasses/{klassId}/teachers”)

publicList<Teacher>getKlassRelatedTeachers(

@PathVariable(“klassId“)Long klassId,

@RequestParam(value=”type”,required=false)String type)

请求的 url 是:/klasses/{123456}/teachers?type=web

服务获取到的数据就是:klassId=123456,type=web。

@RequestBody

用于读取 Request 请求(可能是 POST,PUT,DELETE,GET 请求)的 body 部分并且Content-Type 为 application/json 格式

一个请求方法只可以有一个@RequestBody,但是可以有多个@RequestParam和@PathVariable

读取配置信息

@value

@Value(“${property}”)

@ConfigurationProperties(常用)

通过@ConfigurationProperties读取配置信息并与 bean 绑定。

@Component
@ConfigurationProperties(prefix = "library")
@Data
class LibraryProperties {
@NotEmpty
private String location;
}

发表评论

您的电子邮箱地址不会被公开。