文章目录
1.基本介绍
2.@PathVariable 路径参数获取信息
1.代码实例
1.index.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><h1>基本注解</h1><hr/><ahref="/monster/100/king">@PathVariable-路径变量:/monster/100/king</a></body></html>
2.ParameterController.java
packagecom.sun.springboot.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PathVariable;importorg.springframework.web.bind.annotation.RestController;importjava.util.Map;/**
* @author 孙显圣
* @version 1.0
*/@RestControllerpublicclassParameterController{@GetMapping("/monster/{id}/{name}")//接受两个路径参数publicStringpathVariable(@PathVariable("id")Integer id,@PathVariable("name")String name,@PathVariableMap<String,String> map){//这里的map指将所有的路径参数都放到map中System.out.println("id:"+ id +" name:"+ name);for(Map.Entry<String,String> entry : map.entrySet()){System.out.println("key:"+ entry.getKey()+" value: "+ entry.getValue());}return"success";//返回json给浏览器}}
3.测试
2.细节说明
- @PathVariable(“xxx”)必须跟{xxx}相对应
- 可以将所有的路径参数放到map中 @PathVariable Map<String, String> map
3.@RequestHeader 请求头获取信息
1.代码实例
1.index.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><h1>基本注解</h1><hr/><ahref="/requestHeader">@RequestHeader-获取请求头信息</a></body></html>
2.ParameterController.java
packagecom.sun.springboot.controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestHeader;importorg.springframework.web.bind.annotation.RestController;importjava.util.Map;/**
* @author 孙显圣
* @version 1.0
*/@RestControllerpublicclassParameterController{@GetMapping("/requestHeader")//获取请求头的信息publicStringrequestHeader(@RequestHeader("host")String host,@RequestHeaderMap<String,String> header){System.out.println("host:"+ host);System.out.println(header);return"success";}}
3.测试
2.细节说明
- 请求头的信息都是以key - value的形式存储的
- 可以通过@RequestHeader(“xxx”)来获取xxx对应的value
- 也可以通过@RequestHeader Map<String, String> header将所有的key - value都封装到map中
4.@RequestParameter 请求获取参数信息
1.代码实例
1.index.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><h1>基本注解</h1><hr/><ahref="/hi?hobby=打篮球&hobby=踢球">@RequestParam-请求参数</a></body></html>
2.ParameterController.java
packagecom.sun.springboot.controller;importorg.springframework.web.bind.annotation.*;importjava.util.List;/**
* @author 孙显圣
* @version 1.0
*/@RestControllerpublicclassParameterController{@GetMapping("/hi")publicStringhi(@RequestParam(value ="name", defaultValue ="孙显圣")String name,@RequestParam("hobby")List<String> list){System.out.println("name:"+ name);System.out.println(list);return"success";}}
3.测试
2.细节说明
- 请求参数是可以设置默认值的,使用defaultValue属性即可
- 请求参数还可以将同名的结果封装到List中
- 请求参数也可以使用@RequestParameter Map<String, String> map 将所有参数封装到map中,但是如果有同名的结果只会得到第一个,因为map的key是唯一的
5.@CookieValue cookie获取值
1.代码实例
1.index.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><h1>基本注解</h1><hr/><ahref="/cookie">@CookieValue-获取cookie的值</a></body></html>
2.ParameterController.java
packagecom.sun.springboot.controller;importorg.springframework.web.bind.annotation.*;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServletRequest;/**
* @author 孙显圣
* @version 1.0
*/@RestControllerpublicclassParameterController{@GetMapping("/cookie")//这里可以设置required = false意为不是必须存在的,如果不存在则得到的值就为null//如果后面的参数类型是Cookie,则会获取Cookie对象并封装到变量中publicStringcookie(@CookieValue(value ="cookie_key", required =false)String cookie_value,@CookieValue(value ="username", required =false)Cookie cookie,HttpServletRequest request){//使用原生api获取cookiesCookie[] cookies = request.getCookies();for(Cookie cookie1 : cookies){System.out.println(cookie1);}System.out.println(cookie_value);System.out.println("name:"+ cookie.getName()+" value: "+ cookie.getValue());return"success";}}
3.测试
2.细节说明
- @CookieValue可以根据后面要封装的参数的类型来获取指定的值,如果后面的类型是Cookie类型则会获取一个Cookie对象并封装进入,如果是String类型则会获取Cookie的value来进行封装
- 还可以通过Servlet原生api的request来获取所有的cookie
- @CookieValue中有属性required默认为true,意为必须存在,否则报错,如果设置为false,则如果获取不到则为null
6.@RequestBody 处理json请求,post请求体获取信息
1.代码实例
1.index.html
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>Title</title></head><body><h1>基本注解</h1><hr/><formaction="/requestBody"method="post"><inputtype="text"name="username"><br><inputtype="text"name="password"><br><inputtype="submit"value="submit"></form></body></html>
2.ParameterController.java
packagecom.sun.springboot.controller;importorg.springframework.web.bind.annotation.*;/**
* @author 孙显圣
* @version 1.0
*/@RestControllerpublicclassParameterController{@PostMapping("requestBody")publicStringgetRequestBody(@RequestBodyString requestBody){//获取请求体System.out.println(requestBody);return"success";}}
3.测试
7.@RequestAttribute 请求域获取信息
1.代码实例
1.RequestController.java
packagecom.sun.springboot.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestAttribute;importorg.springframework.web.bind.annotation.ResponseBody;importjavax.servlet.http.HttpServletRequest;/**
* @author 孙显圣
* @version 1.0
*/@ControllerpublicclassRequestController{@GetMapping("/login")publicStringlogin(HttpServletRequest request){//在Request域中存放一些信息
request.setAttribute("name","sun");
request.setAttribute("age",13);//调用视图解析器,请求转发到/okreturn"forward:/ok";}@ResponseBody@GetMapping("/ok")publicStringok(@RequestAttribute(value ="name", required =false)String name){//使用注解来获取请求域中的信息并封装到参数中System.out.println("name: "+ name);return"success";//返回json给浏览器}}
2.配置视图解析器 application.yml
spring:mvc:view:#配置了视图解析器suffix: .html #后缀prefix: / #前缀,指的是根目录
3.测试
8.@SessionAttribute session域获取信息
1.代码实例
1.SessionController.java
packagecom.sun.springboot.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.ResponseBody;importorg.springframework.web.bind.annotation.SessionAttribute;importjavax.servlet.http.HttpServletRequest;/**
* @author 孙显圣
* @version 1.0
*/@ControllerpublicclassSessionController{@GetMapping("/login")publicStringlogin(HttpServletRequest request){//在session域中设置信息
request.getSession().setAttribute("session","session_value");//调用视图解析器,请求转发到/okreturn"forward:/ok";}@ResponseBody@GetMapping("/ok")publicStringok(@SessionAttribute(value ="session")String value){//使用注解来获取session域中的信息并封装到参数中System.out.println("session: "+ value);return"success";//返回json给浏览器}}
2.配置视图解析器(同上)
3.测试
9.复杂参数
1.代码实例
1.RequestController.java
packagecom.sun.springboot.controller;importorg.springframework.stereotype.Controller;importorg.springframework.ui.Model;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestAttribute;importorg.springframework.web.bind.annotation.ResponseBody;importjavax.servlet.http.HttpServletResponse;importjava.util.Map;/**
* @author 孙显圣
* @version 1.0
*/@ControllerpublicclassRequestController{@GetMapping("/login")publicStringlogin(Map<String,Object> map,Model model,HttpServletResponse response){//给map封装信息
map.put("user","sun");
map.put("job","工程师");//model封装信息
model.addAttribute("sal",1000);//结果最后都会封装到request域中//调用视图解析器,请求转发到/okreturn"forward:/ok";}@ResponseBody@GetMapping("/ok")publicStringok(@RequestAttribute("user")String user,@RequestAttribute("job")String job,@RequestAttribute("sal")Integer sal){//使用注解来获取请求域中的信息并封装到参数中System.out.println("user:"+ user +" job:"+ job +" sal:"+sal);return"success";//返回json给浏览器}}
2.测试
2.HttpServletResponse给浏览器设置cookie
1.代码实例
packagecom.sun.springboot.controller;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.CookieValue;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.ResponseBody;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServletResponse;/**
* @author 孙显圣
* @version 1.0
*/@ControllerpublicclassRequestController{@GetMapping("/login")publicStringlogin(HttpServletResponse response){Cookie cookie =newCookie("cookie_name","cookie_value");
response.addCookie(cookie);//调用视图解析器,重定向到/ok,不能使用请求转发,因为虽然响应给客户端cookie了,// 但是由于是请求转发,第二个controller得到的是最开始的请求,那时候还没有cookiereturn"redirect:/ok";}@ResponseBody@GetMapping("/ok")publicStringok(@CookieValue("cookie_name")Cookie cookie){//获取cookieSystem.out.println("key:"+ cookie.getName()+" value:"+ cookie.getValue());return"success";//返回json给浏览器}}
2.测试
本文转载自: https://blog.csdn.net/m0_64637029/article/details/136713451
版权归原作者 S-X-S 所有, 如有侵权,请联系我们删除。
版权归原作者 S-X-S 所有, 如有侵权,请联系我们删除。