栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot-接收参数相关注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot-接收参数相关注解

SpringBoot-接收参数相关注解 1.基本介绍
  1. SpringBoot 接收客户端提交数据/参数会使用到相关注解

  2. 详 解 @PathVariable 、 @RequestHeader 、 @ModelAttribute 、 @RequestParam 、 @MatrixVariable、@CookieValue、@RequestBody

2.接收参数相关注解应用实例 1.需求: 演示各种方式提交数据/参数给服务器,服务器如何使用注解接收 2.应用实例演示

● 需求: 演示各种方式提交数据/参数给服务器,服务器如何使用注解接收

创建srcmainresourcesstaticindex.html




    
    index


hello, llp

基本注解:
@PathVariable-路径变量 monster/200/jack

@PathVariable 使用

演示@PathVariable 使用,创建 srcmainjavacomllpspringbootcontrollerParameterController.java, 完成测试

@RestController
public class ParameterController {

    
    @GetMapping("/monster/{id}/{name}")
    public String pathVariable(@PathVariable("id") Integer id,
                               @PathVariable("name") String name,
                               @PathVariable Map map) {
        System.out.println("id-" + id);
        System.out.println("name-" + name);
        System.out.println("map-" + map);
        return "success";
    }

}

@RequestHeader 使用

演示@RequestHeader 使用,修改 ParameterController.java , 完成测试

√ 修改 index.html

@RequestHeader-获取Http请求头 

√ 修改 ParameterController.java

@GetMapping("https://blog.csdn.net/requestHeader")
public String requestHeader(@RequestHeader("host") String host,
                            @RequestHeader Map header,
                            @RequestHeader("accept") String accept) {
    System.out.println("host-" + host);
    System.out.println("header-" + header);
    System.out.println("accept-" + accept);
    return "success";
}

@RequestParam 使用

演示@RequestParam 使用,修改 ParameterController.java , 完成测试

√ 修改 index.html

@RequestParam-获取请求参数

√ 修改 ParameterController.java

@GetMapping("/hi")
public String hi(@RequestParam(value = "name") String username,
                 @RequestParam("fruit") List fruits,
                 @RequestParam Map paras) {
    //username-wukong
    System.out.println("username-" + username);
    //fruit-[apple, pear]
    System.out.println("fruit-" + fruits);
    //paras-{name=wukong, fruit=apple, id=300, address=北京}
    System.out.println("paras-" + paras);
    return "success";
}
@CookieValue 使用

演示@CookieValue 使用,修改 ParameterController.java , 完成测试

√ 修改 index.html

@CookieValue-获取cookie值

√ 修改 ParameterController.java

@GetMapping("https://blog.csdn.net/cookie")
public String cookie(@CookieValue(value = "cookie_key", required = false) String cookie_value,
                     HttpServletRequest request,
                     @CookieValue(value = "username", required = false) Cookie cookie) {
    System.out.println("cookie_value-" + cookie_value);
    if (cookie != null) {
        System.out.println("username-" + cookie.getName() + "-" + cookie.getValue());
    }
    System.out.println("-------------------------");
    Cookie[] cookies = request.getCookies();
    for (Cookie cookie1 : cookies) {
        System.out.println(cookie1.getName() + "=>" + cookie1.getValue());
    }
    return "success";
}

@RequestBody 使用

演示@RequestBody 使用,修改 ParameterController.java , 完成测试

√ 修改 index.html


测试@RequestBody获取数据: 获取POST请求体

姓名:
年龄:

√ 修改 ParameterController.java

@PostMapping("/save")
public String postMethod(@RequestBody String content) {
    System.out.println("content-" + content);
    return "success";
}
@RequestAttribute 和 @SessionAttribute使用

演示@RequestAttribute @SessionAttribute使用,创建 com/hspedu/web/controller/RequestController.java , 完成测试

√ 修改 index.html

@RequestAttribute、@SessionAttribute-获取request域、session属性-

√ 创建 RequestController.java

    @GetMapping("https://blog.csdn.net/login")
    public String login(HttpServletRequest request) {
        request.setAttribute("user", "llp");
        //向session中添加数据
        request.getSession().setAttribute("website", "http://www.baidu.com");
        //这里需要使用forward关键字,如果不适用则会走视图解析器,这
        //里视图解析器前缀配置的是/  后缀配置的.html  ---> /ok.html
        //而请求转发在服务器端执行,/被解析成 ip:port/工程路径
        //进而最终得到的完整路径是 ip:port/工程路径/ok.html
        //但是我们这里希望访问的是 ip:port/工程路径/ok这个请求路径
        //因此这里手动的设置forward:/ok ,底层会根据我们设置的路径进行请求转发
        return "forward:/ok";
    }

    @GetMapping("ok")
    //返回字符串,不走视图解析器
    @ResponseBody
    public String ok(@RequestAttribute(value = "user", required = false) String username,
                     @SessionAttribute(value = "website",required = false) String website, HttpServletRequest request) {
        System.out.println("username= " + username);
        System.out.println("通过servlet api 获取 username-" +  request.getAttribute("user"));

        System.out.println("website = " + website);
        System.out.println("通过servlet api 获取 website-"+request.getSession().getAttribute("website"));
        return "success";
    }
}

3.复杂参数 1.基本介绍
  1. 在开发中,SpringBoot 在响应客户端请求时,也支持复杂参数
  2. Map、Model、Errors/BindingResult、RedirectAttributes、ServletResponse、SessionStatus、 UriComponentsBuilder、ServletUriComponentsBuilder、HttpSession
  3. Map、Model 数据会被放在 request 域, 底层 request.setAttribute()
  4. RedirectAttributes 重定向携带数据
2.复杂参数应用实例

####1.说明 : 演示复杂参数的使用,重点: Map、Model、ServletResponse

2.代码实现
//响应一个注册请求
@GetMapping("/register")
public String register(Map map,
                       Model model,
                       HttpServletResponse response) {
    //如果一个注册请求,会将注册数据封装到map或者model
    //map中的数据和model的数据,会被放入到request域中
    map.put("user","llp");
    map.put("job","码农");
    model.addAttribute("sal", 2500);
    //一会我们再测试response使用
    //我们演示创建cookie,并通过response 添加到浏览器/客户端
    Cookie cookie = new Cookie("email", "123@sohu.com");
    response.addCookie(cookie);

    //请求转发
    return "forward:/registerOk";

}

@ResponseBody
@GetMapping("/registerOk")
public String registerOk(HttpServletRequest request) {

    System.out.println("user-" + request.getAttribute("user"));
    System.out.println("job-" + request.getAttribute("job"));
    System.out.println("sal-" + request.getAttribute("sal"));

    return "success";
}

4.自定义对象参数-自动封装 1.基本介绍
  1. 在开发中,SpringBoot 在响应客户端请求时,也支持自定义对象参数
  2. 完成自动类型转换与格式化
  3. 支持级联封装
2.自定义对象参数-应用实例 1.需求说明 : 演示自定义对象参数使用,完成自动封装,类型转换

2.代码实现

1.创建srcmainresourcesstaticsave.html




    
    添加妖怪

添加妖怪-坐骑[测试封装 POJO;]

编号:
姓名:
年龄:
婚否:
生日:
坐骑:
价格:

2.修改srcmainjavacomllpspringbootcontrollerParameterController.java

@PostMapping("/savemonster")
public String saveMonster(Monster monster) {
    System.out.println("monster= " + monster);
    return "success";
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1035653.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号