springmvc 控制器
有@Controller,@RestController注解的类叫做控制器, 项目中控制器有很多·般相关的业务是一个控制器, 比如订单的处理工作, 订单的控制OrderController
@RestControllerx别2.@Controller,
@RestController包含了@Controller的功能,同时加入了ResponseBody的注解。表示当前控制器类中的所有方法,都会默认加入@ResponseBody的功能。 方法的返回值是数据可以通过HttpServletResponse输出给浏览器,请求方法。
@Controller注解有@Component的功能,控制器类对象是Spring容器管理的。
3.定制控制器方法
1.方法上面的@RequestMapping(value=“请求的uri地址”) : 表示这个uri的请求由当前方法处理aGetMapping,@PostMapping,@PutMapping ,@DeleteMapping
2.控制器方法的形参,接收请求的参数,多种方法接收参数
3.控制器方法的返回值,表示应答结果,给请求的结果(数据,视图)
@RequestMapping
@RequestMapping:用于将 web 请求映射到控制器类的方法。此方法处理请求。可用在类上或方法上。
在类和方法同时组合使用。
重要的属性
value:别名 path 表示请求的 ri, 在类和方法方法同时使用 value,方法上的继承类上的 value 值。
method: 请求方式,支持 GET.POSTHEAD.OPTIONSPUTPATCHDELETETRACE。
值为: RequestMethod[]method0 ,RequestMethod 是enum 类型。
快捷注解
@GetMapping:表示get 请求方式的@RequestMapping@PostMapping:表示 post 请求方式的@RequestMapping@PutMapping:表示put 请求方式的@RequestMapping@DeleteMapping:表示 delete 请求方式的@RequestMapping
对于请求方式 get,post,put,delete 等能够 HttMethod 表示,Spring Boot3 之前他是enum,Spring Boot3他是 class
public enum HttpMethodSpring Boot3之前他是enumpublic final class HttpMethod Spring Boot3 他是 class
请求参数类型
响应参数类型
请求路径
//使用? 匹配单个字符
//http://localhost:8080/file/test.html
@RequestMapping ("/file/t?st.html")
public String path1(HttpServletRequest request){
return "path1=" + request.getRequestURI();
}
//使用 * 匹配多个字符,不能匹配路径
//http://localhost:8080/file/test/cat.html
@RequestMapping ("/file/test/*.html")
public String path2(HttpServletRequest request){
return "path2=" + request.getRequestURI();
}
//使用 ** 匹配所有字符
//http://localhost:8080/file/dog/cat
@RequestMapping ("/file/**")
public String path3(HttpServletRequest request){
return "path3=" + request.getRequestURI();
}
//路径变量{*变量名称}
//http://localhost:8080/file/dog/cat
@GetMapping ("/file/{*id}")
public String path4(@PathVariable("id") String id, HttpServletRequest request){
return "path4=" + request.getRequestURI() + ", id=" + id;
}
//正则表达式
//http://localhost:8080/pages/111.log
@GetMapping ("/pages/{fname:\\w+}.log")
public String path5(@PathVariable("fname") String fname, HttpServletRequest request){
return "path5=" + request.getRequestURI() + ", fname=" + fname ;
}
接收请求的方式
用户在浏览器中点击按钮时,会发送一个请求给服务器,其中包含让服务器程序需要做什么的参数。这些参数发送给控制器方法。控制器方法的形参列表接受请求参数。
接受参数方式:
请求参数与形参一一对应,适用简单类型。形参可以有合适的数据类型,比如 String,Integer ,int等
//一一对应,适合接收简单类型数据String,int,long,double,float, 参数数量少,比如2,3
//http://Tocahost:8080/p?name=lisi&age=20&sex=m
@GetMapping("/param/p1")
public String p1(String name,Integer age,String sex{
return "接收参数:" + name+","+age+","+sex;
}
对象类型,控制器方法形参是对象,请求的多个参数名与属性名相对应。
//使用对象接收参数, 要求对象的属性名称和请求中参数名一样, 属性有set方法,类有无参数构造方法@GetMapping("/param/p2")
public String p2(Person person,String type){
person.getName();
person.getSex();
person.getAge();
//xxxService(person .getName(),person.getSex())
return "接收参数,使用对象:"+person.toString()+",type="+type;
@RequestParam注解,将查询参数,form 表单数据解析到方法参数,解析 multipart 文件上传
//使用@RequestParam
@GetMapping("/param/p4")
public String p4(@RequestParam(value="name",required = true) String name@RequestParam(value = "age",required = false,defaultValue = "26") Integer age){
return "p4,name="+name+",age="+age;
}
@RequestBody,接受前端传递的ison格式参数
//使用@RequestBody接收json, post请求体数据
/**
前端数据: {"username":"lisi","age":2日}@RequestBody: 从请求体中读取json数,将数转为形参对象的属性值X
框架创建User对象,将username,age key的值赋值给两个同名的属性前端请求中需要指定:义 Content-Type: application/json*
*/
@PostMapping("/param/json")
public String p6(@RequestBody User user){
return "p6,json:"+user.toString();
}
HttpServletRequest 使用 request 对象接受参数,request.getParameter(“…)
//使用HttpServletRequest接收参数@GetMapping(@*"/param/p3")
public String p3(HttpServletRequest request){
String name = request.getParameter( name: "name");
String age = request.getParameter( name: "age");
String sex = request.getParameter( name:"sex");
return "name="+name+",age="+age+",sex="+sex;
}
@RequestHeader,从请求 header 中获取某项值
//使用@RequestHeader
@GetMapping("/param/p5")
public String p5(@RequestParam(value="name",required = true) String name@RequestParam(value = "age",required = false,defaultValue = "26") Integer age,@RequestHeader("Acceptr) String accept){
return "p5,name="+name+",age="+age + "Accept:"+accept;
}
Reader
//使用Reader ,InputStream 读取post请求体的数据
@PostMapping("/param/json2")
public String p6(Reader reader){
StringBuffer content = new StringBuffer("");
try(BufferedReader bin = new BufferedReader(reader)){
var line = "";
while ( (line = bin.readLine()) != null){
content.append(line);
}catch (IOException e){
e.printStackTrace();
}
return "p7,reader=" + content.toString();
接收数组
//接收数组 多个值
@GetMapping("/param/vals")
public String getMulitVal(Integer[] id){
List<Integer> ids = Arrays.stream(id).toList();
return ids.toString();
}
解析参数需要的值,SpringMVC 中专门有个接口来于这个事情,这个接口就是: HandlerMethodArgumentResolver:中文称呼:处理器方法参数解析器,说白了就是解析请求得到 Controller 方法的参数的值。