当前位置:   article > 正文

10.SpringBoot(接受参数相关注解)

10.SpringBoot(接受参数相关注解)

常用注解

@PathVariable 路径参数获取信息

<a href="/monster/100/king">@PathVariable-路径变量:/monster/100/king</a>

  1. package com.sun.springboot.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.Map;
  6. @RestController
  7. public class ParameterController {
  8. @GetMapping("/monster/{id}/{name}") //接受两个路径参数
  9. public String pathVariable(@PathVariable("id") Integer id, @PathVariable("name") String name,
  10. @PathVariable Map<String, String> map) { //这里的map指将所有的路径参数都放到map中
  11. System.out.println("id:" + id + " name:" + name);
  12. for (Map.Entry<String, String> entry : map.entrySet()) {
  13. System.out.println("key:" + entry.getKey() + " value: " + entry.getValue());
  14. }
  15. return "success"; //返回json给浏览器
  16. }
  17. }

 @RequestHeader 请求头获取信息

  1. package com.sun.springboot.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RequestHeader;
  4. import org.springframework.web.bind.annotation.RestController;
  5. import java.util.Map;
  6. @RestController
  7. public class ParameterController {
  8. @GetMapping("/requestHeader") //获取请求头的信息
  9. public String requestHeader(@RequestHeader("host") String host, @RequestHeader Map<String, String> header) {
  10. System.out.println("host:" + host);
  11. System.out.println(header);
  12. return "success";
  13. }
  14. }

@RequestParameter 请求获取参数信息

<a href="/hi?hobby=打篮球&hobby=踢球">@RequestParam-请求参数</a>

  1. package com.sun.springboot.controller;
  2. import org.springframework.web.bind.annotation.*;
  3. import java.util.List;
  4. @RestController
  5. public class ParameterController {
  6. @GetMapping("/hi")
  7. public String hi(@RequestParam(value = "name", defaultValue = "xxx") String name,
  8. @RequestParam("hobby") List<String> list) {
  9. System.out.println("name:" + name);
  10. System.out.println(list);
  11. return "success";
  12. }
  13. }

@CookieValue cookie获取值

<a href="/cookie">@CookieValue-获取cookie的值</a>

  1. package com.sun.springboot.controller;
  2. import org.springframework.web.bind.annotation.*;
  3. import javax.servlet.http.Cookie;
  4. import javax.servlet.http.HttpServletRequest;
  5. @RestController
  6. public class ParameterController {
  7. @GetMapping("/cookie")
  8. //这里可以设置required = false意为不是必须存在的,如果不存在则得到的值就为null
  9. //如果后面的参数类型是Cookie,则会获取Cookie对象并封装到变量中
  10. public String cookie(@CookieValue(value = "cookie_key", required = false) String cookie_value,
  11. @CookieValue(value = "username" , required = false) Cookie cookie, HttpServletRequest request) {
  12. //使用原生api获取cookies
  13. Cookie[] cookies = request.getCookies();
  14. for (Cookie cookie1 : cookies) {
  15. System.out.println(cookie1);
  16. }
  17. System.out.println(cookie_value);
  18. System.out.println("name:" + cookie.getName() + " value: " + cookie.getValue());
  19. return "success";
  20. }
  21. }

@RequestBody 处理json请求,post请求体获取信息

<form action="/requestBody" method="post">
    <input type="text" name="username"><br>
    <input type="text" name="password"><br>
    <input type="submit" value="submit">
</form>

  1. @RestController
  2. public class ParameterController {
  3. @PostMapping("requestBody")
  4. public String getRequestBody(@RequestBody String requestBody) { //获取请求体
  5. System.out.println(requestBody);
  6. return "success";
  7. }
  8. }

@RequestAttribute 请求域获取信息

  1. @Controller
  2. public class RequestController {
  3. @GetMapping("/login")
  4. public String login(HttpServletRequest request) {
  5. //在Request域中存放一些信息
  6. request.setAttribute("name", "sun");
  7. request.setAttribute("age", 13);
  8. //调用视图解析器,请求转发到/ok
  9. return "forward:/ok";
  10. }
  11. @ResponseBody
  12. @GetMapping("/ok")
  13. public String ok(@RequestAttribute(value = "name", required = false) String name) { //使用注解来获取请求域中的信息并封装到参数中
  14. System.out.println("name: " + name);
  15. return "success"; //返回json给浏览器
  16. }
  17. }

2.配置视图解析器 application.yml

spring:
  mvc:
    view: #配置了视图解析器
      suffix: .html #后缀
      prefix: / #前缀,指的是根目录

@SessionAttribute session域获取信息

  1. @Controller
  2. public class SessionController {
  3. @GetMapping("/login")
  4. public String login(HttpServletRequest request) {
  5. //在session域中设置信息
  6. request.getSession().setAttribute("session", "session_value");
  7. //调用视图解析器,请求转发到/ok
  8. return "forward:/ok";
  9. }
  10. @ResponseBody
  11. @GetMapping("/ok")
  12. public String ok(@SessionAttribute(value = "session") String value) { //使用注解来获取session域中的信息并封装到参数中
  13. System.out.println("session: " + value);
  14. return "success"; //返回json给浏览器
  15. }
  16. }

2.配置视图解析器(同上)

复杂参数处理

@RequestAttribute

  1. package com.sun.springboot.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.ui.Model;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.RequestAttribute;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import javax.servlet.http.HttpServletResponse;
  8. import java.util.Map;
  9. @Controller
  10. public class RequestController {
  11. @GetMapping("/login")
  12. public String login(Map<String, Object> map, Model model, HttpServletResponse response) {
  13. //给map封装信息
  14. map.put("user", "sun");
  15. map.put("job", "工程师");
  16. //model封装信息
  17. model.addAttribute("sal", 1000);
  18. //结果最后都会封装到request域中
  19. //调用视图解析器,请求转发到/ok
  20. return "forward:/ok";
  21. }
  22. @ResponseBody
  23. @GetMapping("/ok")
  24. public String ok(@RequestAttribute("user") String user, @RequestAttribute("job") String job,
  25. @RequestAttribute("sal") Integer sal) { //使用注解来获取请求域中的信息并封装到参数中
  26. System.out.println("user:" + user + " job:" + job + " sal:" +sal);
  27. return "success"; //返回json给浏览器
  28. }
  29. }

@CookieValue

  1. package com.sun.springboot.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.CookieValue;
  4. import org.springframework.web.bind.annotation.GetMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import javax.servlet.http.Cookie;
  7. import javax.servlet.http.HttpServletResponse;
  8. @Controller
  9. public class RequestController {
  10. @GetMapping("/login")
  11. public String login(HttpServletResponse response) {
  12. Cookie cookie = new Cookie("cookie_name", "cookie_value");
  13. response.addCookie(cookie);
  14. //调用视图解析器,重定向到/ok,不能使用请求转发,因为虽然响应给客户端cookie了,
  15. // 但是由于是请求转发,第二个controller得到的是最开始的请求,那时候还没有cookie
  16. return "redirect:/ok";
  17. }
  18. @ResponseBody
  19. @GetMapping("/ok")
  20. public String ok(@CookieValue("cookie_name") Cookie cookie) {
  21. //获取cookie
  22. System.out.println("key:" + cookie.getName() + " value:" + cookie.getValue());
  23. return "success"; //返回json给浏览器
  24. }
  25. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/416082
推荐阅读
相关标签
  

闽ICP备14008679号