SpringMVC中@Controller注解类下常用注解的理解及代码解释

SpringMVC中常用方法上的注解

@Controller(类上面的注解)

  • @Controller注解标注是一个类是Web控制器,其和@Component注解等价,只不过在Web层使用,其便于区分类的作用。

@RequestMapping

@RequestMapping注解能够处理的HTTP请求方法有: GET, HEAD, POST, PUT, PATCH, DELETE,OPTIONS, TRACE 。

  • @RequestMapping是Spring Web应用程序中最常被用到的注解之一。
  • 在对SpringMVC进行配置的时候,需要指定请求与处理方法之间的映射关系,这时候就需要使用@RequestMapping注解。该注解可以在控制器类的级别和其方法级别上使用
package org.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class TestController {
    
    
	@RequestMapping("/1")
	public String test1(){
    
    
		return "redirect:/home.html";
	}
}
  • 以上方法提供的服务路径为 /test/1 。也就是类和方法上@RequestMapping配置的路径相加。此时可以使用任何请求方法,且发生了重定向,地址栏URL会发生变化。
  • 当然也可以单独在方法上面使用该注解

@ResponseBody

  • 由名字也可以知道响应Body(响应体),加上该注解就表示,最后返回的的内容在响应体当中。

组合注解

  • @RestController就代表是(@Controller和@RequestMapping)这俩个同时注解;

控制器方法支持的参数类型

1.@PathVariable

能够改变URL;

  • 一般的 URI 服务路径都是固定的,SpringMVC提供了 restful 风格可以变化的 URI。
@GetMapping("/owners/{ownerId}/pets/{petId}")
	public String findPet(@PathVariable Long ownerId, @PathVariable Long petId) {
    
    
		return "主人id:"+ownerId+", 宠物id:"+petId;
	}

说明:

  • {}是将服务路径 URI 中的部分定义为变量,之后在方法参数中获取该路径变量。
  • 请求 /arg/owners/1/pets/2 ,显示的网页内容为: 主人id:1, 宠物id:2 。
  • 变量已经定义了为Long长整形,所以不能转换为Long的 URI 都会报错,如请求/arg/owners/abc/pets/2 就会报错,响应状态码400。
  • 变量名ownerId,petId必须和 URI 中的定义名称一致。

2.@RequestParam

当请求数据要绑定到某个简单对象时,可以使用@RequestParam。

  • URL 中的请求数据queryString
  • 请求头,Content-Type为表单默认提交的格式 application/x-www-form-urlencoded ,请求体中的数据
  • 请求头,Content-Type为 multipart/form-data ,请求体中的数据。 form-data 可以提交文本数据,也可以提交二进制文件。
  • 以上简单对象包括:基本数据类型、包装类型、MultipartFile(接收二进制文件)

需要注意@RequestParam注解参数默认为 required=true ,如果不传该参数就会报错,需要指定为: @RequestParam(required = false)
例如:通过 post 请求,请求数据的键分别为 username 和 password ,指定为queryString,或 Content-Type为表单数据类型, form-data 都可以:
加上这个注解,就可以获取不管是是queryString中的,还是请求头中,键为username的值(value);password同样的道理

@PostMapping("/param1")
public Object param1(@RequestParam String username,@RequestParam String password){
    
    
	Map<String, String> map = new HashMap<>();
	map.put("用户名", username);
	map.put("密码", password);
	return map;
}

3.POJO对象

  • POJO(Plain Ordinary Java Object):简单的 java 对象,实际就是属性提供了Getter,Setter方法的普通对象。

使用 java 对象和使用@RequestParam注解非常类似,只是有点细节不同:

  • @RequestParam是以方法参数变量名和传入的键对应POJO对象作为方法参数时,是以POJO对象中的属性名对应传入的键
  • @RequestParam默认必须传入该请求数据,而 POJO 对象是根据请求数据来填充属性,如果请求数据没有,则属性就是默认值(new对象时每个属性的默认值)。
@PostMapping("/pojo1")
public Object pojo1(String username, Integer count){
    
    
	Map<String, String> map = new HashMap<>();
	map.put("用户名", username);
	map.put("count", String.valueOf(count));
	return map;
}
  • 总的来说:@RequestParam和POJO对象常用的是POJO对象

4.@RequestPart

  • 对于请求的数据类型Content-Type为 multipart/form-data 时,二进制文件除了以上@RequestParam和 POJO 对象的方式外,可以使用@RequestPart。
@PostMapping("/part")
public Object part(User user, @RequestPart MultipartFile file) throws
IOException {
    
    
	Map<String, String> map = new HashMap<>();
	map.put("用户名", user.getUsername());
	map.put("密码", user.getPassword());
	map.put("文件名", file.getName()+", "+file.getOriginalFilename());
	map.put("文件类型", file.getContentType());
	map.put("文件大小", file.getSize()/1024+"KB");
	map.put("文件内容(二进制转字符串)", new String(file.getBytes()));
	return map;
}

当然,上面的@RequestParam和POJO对象也可以获取文件和@RequestPart用法一样。因此这个注解就不常用,用的都是POJO对象

猜你喜欢

转载自blog.csdn.net/qq_45665172/article/details/113665680