分享

SpringBoot普通参数与基本注解(@PathVariable、@RequestHeader、@RequestParam、@RequestBody)

 印度阿三17 2021-03-10

@PathVariable(获取路径变量)

index.html:        基本注解测试测试基本注解:

@PathVariable(路径变量):

@RequestParam(获取请求参数)

@RequestHeader(获取请求头)

@CookieValue(获取cookie值)

@RequestBody(获取请求体[POST])

ParameterTestController类:@RestController public class ParameterTestController {@GetMapping("user/{id}/pet/{petName}")//映射html中的链接地址——href="user/3/pet/tom"     public Map getUser(@PathVariable() String id,//获取路径变量id的值                                       @PathVariable() String petName,                                       @PathVariable Map pv){//获取链接中全部路径变量         Map map=new HashMap<>();         map.put("id",id);         map.put("petName",petName);         map.put("pv",pv);         return map;     } }

结果:

在这里插入图片描述

@RequestParam(获取请求参数)

index.html@RequestParam(获取请求参数)

ParameterTestController类添加: @GetMapping("user/zhangsan")//映射html中的链接地址——href="user/zhangsan?age=18&interests=basketball&interests=game"     public Map getUser2(@RequestParam("age") Integer age,                                        @RequestParam("interests") List interests,                                        @RequestParam Map params){Map map2=new HashMap<>();         map2.put("age",age);         map2.put("interests",interests);         map2.put("params",params);         return map2;     }

结果:

在这里插入图片描述

@RequestHeader(获取请求头)

index. html:@RequestHeader(获取请求头)

ParameterTestController类添加:  @GetMapping("user/RequestHeader")     public Map getUser3(@RequestHeader("Accept") String Accept,                                        @RequestHeader Map header){Map map=new HashMap<>();         map.put("Accept",Accept);         map.put("header",header);         return map;

结果:

在这里插入图片描述

@RequestBody(获取请求体[POST])

index.html    测试@RequestBody获取数据 

用户名: 

邮箱:    

ParameterTestController类添加:@PostMapping("/save")     public Map postMethod(@RequestBody String content){Map map = new HashMap<>();         map.put("content",content);         return map;     }

结果:

在这里插入图片描述

来源:https://www./content-4-885401.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多