spring boot 常用注解@RestController @RequestMapping@PathVariable@RequestParam@RequestBody


常用的重要有@RestController、 @RequestMapping、@PathVariable、@RequestParam 以及
@RequestBody。主要介绍这几个注解常用的使用方式和特点。

1、@RestController

@RestController 是 Spring Boot 新增的一个注解,包含了原来的 @Controller 和 @ResponseBody 注
解,@ResponseBody 注解是将返回的数据结构转换为 Json 格式。所以 @RestController可以看作是 @Controller 和 @ResponseBody 的结合体,使用@RestController 之后就不用再使用 @Controller 了。如果是前后端分离,这种情况可以直接使用@RestController 将数据以 json 格式传给前端;如果不是前后端分离,一般 Controller 中都会返回到具体的页面,那么此时就不能使用@RestController 了,比如:

public String getUser() {
return "user";
}

其实是需要返回到 user.html 页面的,如果使用 @RestController 的话,会将 user 作为字符串返回的,所以这时候我们需要使用 @Controller 注解。

2、@RequestMapping

@RequestMapping 是一个用来处理请求地址映射的注解,它可以用于类上,也可以用于方法上。在类的级别上的注解会将一个特定请求或者请求模式映射到一个控制器之上,表示类中的所有响应请求的方法都是以该地址作为父路径;在方法的级别表示进一步指定到处理方法的映射关系。
该注解有 6 个属性,一般在项目中比较常用的有三个属性:value、method 和produces。

  1. value 属性:指定请求的实际地址,value 可以省略不写
  2. method 属性:指定请求的类型,主要有 GET、PUT、POST、DELETE,默认为 GET
  3. produces 属性:指定返回内容类型,如 produces = “application/json;charset=UTF-8”
@RestController
@RequestMapping(value = "/test", produces ="application/json; charset=UTF-8")
public class TestController {
@RequestMapping(value = "/get", method = RequestMethod.GET)
	public String testRequest() {
		return "ok";
	}
}

针对四种不同的请求方式,是有相应注解的,不用每次在 @RequestMapping 注解中加method 属性来指定,上面的 GET 方式请求可以直接使用 @GetMapping("/get") 注解,效果一样。相应地,PUT 方式、POST 方式和 DELETE 方式对应的注解分别为@PutMapping、@PostMapping 和DeleteMapping。

3、@PathVariable

@PathVariable 注解主要是用来获取 url 参数,Spring Boot 支持 restfull 风格的 url,比如一个 GET 请求携带一个参数 id 过来,我们将 id 作为参数接收,可以使用@PathVariable 注解。如下:

@GetMapping("/user/{id}")
public String testPathVariable(@PathVariableInteger id) {
	System.out.println("获取到的 id 为:" + id);
	return "success";
}

这里需要注意一个问题,如果想要 url 中占位符中的 id 值直接赋值到参数 id 中,需要保证 url 中的参数和方法接收参数一致,否则就无法接收。如果不一致的话,其实也可以解决,需要用 @PathVariable 中的 value 属性来指定对应关系。如下:

@RequestMapping("/user/{idd}")
public String testPathVariable(@PathVariable(value = "idd") Integer id) {
	System.out.println("获取到的 id 为:" + id);
	return "success";
}

对于访问的 url,占位符的位置可以在任何位置,不一定非要在最后,比如这样也行:/xxx/{id}/user。另外,url 也支持多个占位符,方法参数使用同样数量的参数来接收,原理和一个参数是一样的,例如:

@GetMapping("/user/{idd}/{name}")
public String testPathVariable(@PathVariable(value = "idd") Integer id, @PathVariable String name) {
	System.out.println("获取到的 id 为:" + id);
	System.out.println("获取到的 name 为:" + name);
	return "success";
}

如果 url 中的参数和方法中的参数名称不同的话,需要使用 value 属性来绑定两个参数。

4、@RequestParam

@RequestParam 注解顾名思义,也是获取请求参数的,@PathValiable 注解也是获取请求参数的,那么 @RequestParam 和 @PathVariable有什么不同呢?主要区别在于: @PathValiable 是从 url 模板中获取参数值, 即这种风格的 url:http://localhost:8080/user/{id} ;而 @RequestParam 是从 request里面获取参数值,即这种风格的 url:http://localhost:8080/user?id=1 。同样地,url 上面的参数和方法的参数需要一致,如果不一致,也需要使用 value 属性来说明,除了 value 属性外,还有个两个属性比较常用:

  1. required 属性:true 表示该参数必须要传,否则就会报 404 错误,false 表示可有可无。
  2. defaultValue 属性:默认值,表示如果请求中没有同名参数时的默认值。
    从 url 中可以看出,@RequestParam 注解用于 GET 请求上时,接收拼接在 url 中的参数。除此之外,该注解还可以用于 POST 请求,接收前端表单提交的参数,假如前端通过表单提交 username password 两个参数,那我们可以使用 @RequestParam 来接收,用法和上面一样。
@PostMapping("/form1")
public String testForm(@RequestParam String username, @RequestParam String password) {
	System.out.println("获取到的 username 为:" + username);
	System.out.println("获取到的 password 为:" + password);
	return "success";
}

如果表单数据很多,我们不可能在后台方法中写上很多参数,每个参数还要 @RequestParam 注解。针对这种情况,我们需要封装一个实体类来接收这些参数,实体中的属性名和表单中的参数名一致即可。

public class User {
	private String username;
	private String password;
	// set get
}

使用实体接收的话,我们不能在前面加 @RequestParam 注解了,直接使用即可。

@PostMapping("/form2")
public String testForm(User user) {
	System.out.println("获取到的 username 为:" +user.getUsername());
	System.out.println("获取到的 password 为:" +
	user.getPassword());
	return "success";
}

在实际项目中,一般都是封装一个实体类来接收表单数据,因为实际项目中表单数据一
般都很多。

5、@RequestBody

@RequestBody 注解用于接收前端传来的实体,接收参数也是对应的实体,比如前端通过 json 提交传来两个参数 username 和 password,此时我们需要在后端封装一个实体来接收。在传递的参数比较多的情况下,使用 @RequestBody 接收会非常方便。例如:

public class User {
	private String username;
	private String password;
	// set get
}

@PostMapping("/user")
public String testRequestBody(@RequestBody User user) {
	System.out.println("获取到的 username 为:" + user.getUsername());
	System.out.println("获取到的 password 为:" + user.getPassword());
	return "success";
}

@RequestBody 注解用于 POST 请求上,接收 json 实体参数。它和上面的表单提交有点类似,只不过参数的格式不同,一个是 json 实体,一个是表单提交。在实际项目中根据具体场景和需要使用对应的注解即可。

原创文章 9 获赞 9 访问量 680

猜你喜欢

转载自blog.csdn.net/qq_40079749/article/details/105801036