【springmvc】传值的几种方式&&postman接口测试

    最近在用postman测试postman接口,对于springmvc传值这一块,测试了几种常用方式,总结一下。对于postman这个工具的使用也增加了了解。postman测试很棒,有了工具,测试接口,事倍功半。


一、单个参数传递


1.@RequestBody注解

  1. <span style="font-family:'KaiTi_GB2312';font-size:18px;">   /** 
  2.      * 测试单个参数@RequestBody 
  3.      */  
  4.     @CrossOrigin  
  5.     @RequestMapping(value = {"/insertTestParamsRequest"}, method = RequestMethod.GET)  
  6.     @ResponseBody  
  7.     public void insertTestParamsRequest(@RequestBody String name, @RequestBody String age) {  
  8.         System.out.println("name=====" + name);  
  9.         System.out.println("age=====" + age);  
  10.     }  
  11. </span>  

测试请求路径


2.@RequestParam

   常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( ;
该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;

  1. <span style="font-family:'KaiTi_GB2312';font-size:18px;"/** 
  2.      * 测试单个参数@RequestParam 
  3.      */  
  4.     @CrossOrigin  
  5.     @RequestMapping(value = {"/insertTestParams"}, method = RequestMethod.GET)  
  6.     @ResponseBody  
  7.     public void insertTestParams(HttpServletRequest request, @RequestParam String name, @RequestParam String age) {  
  8.         System.out.println("name=====" + name);  
  9.         System.out.println("age=====" + age);  
  10.     }</span>  

请求路径:


3.@PathVariable注解

    路径为resultful风格,将参数当做请求路径。

   当使用@RequestMapping URI template 样式映射时, 即 Url/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。

  1. <span style="font-family:'KaiTi_GB2312';font-size:18px;">   /** 
  2.      * 测试单个参数@PathVariable 
  3.      */  
  4.     @CrossOrigin  
  5.     @RequestMapping(value = {"/insertTest/{name}/{age}"}, method = RequestMethod.GET)  
  6.     @ResponseBody  
  7.     public void insertTestPathVeriable(HttpServletRequest request, @PathVariable("name") String name, @PathVariable String age) {  
  8.         System.out.println("name=====" + name);  
  9.         System.out.println("age=====" + age);  
  10.     }</span>  

上面代码把URI template 中变量 name的值和age的值,绑定到方法的参数上。若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称。


二、传递pojo对象


1.@RequestBody注解

  1. <span style="font-family:'KaiTi_GB2312';font-size:18px;">    /*测试添加实体*/  
  2.     @CrossOrigin  
  3.     @RequestMapping(value = {"/insertEntityTest"}, method = RequestMethod.POST)  
  4.     @ResponseBody  
  5.     public void insertEntityTest(@RequestBody CurriculumScheduleEntity curriculumScheduleEntity) {  
  6.         System.out.println("name=====" + curriculumScheduleEntity.getClassId());  
  7.         System.out.println("age=====" + curriculumScheduleEntity.getTeachclassId());  
  8.     }</span>  

postman通过json格式测试



2.直接写实体

  1. <span style="font-family:'KaiTi_GB2312';font-size:18px;">    /*测试添加实体*/  
  2.     @CrossOrigin  
  3.     @RequestMapping(value = {"/insertTest"}, method = RequestMethod.POST)  
  4.     @ResponseBody  
  5.     public void insertTest(CurriculumScheduleEntity curriculumScheduleEntity) {  
  6.         System.out.println("name=====" + curriculumScheduleEntity.getClassId());  
  7.         System.out.println("age=====" + curriculumScheduleEntity.getWeekId());  
  8.     }</span>  

form表单测试
  1. <span style="font-family:'KaiTi_GB2312';font-size:18px;"><div>  
  2.     <form action="/curriculumSchedule/insertTest" method="post">  
  3.          classId :<input  name="classId"><br>  
  4.         teachClassId:<input   name="weekId"><br>  
  5.         <input type="submit" value="提交">  
  6.     </form>  
  7. </div></span>  

postman测试格式


三、postman测试List类型参数

以List<string>为例,测试批量删除方法,参数为List<String>。写这个其实没有什么技术,但是中午在测试List接口的时候,用postman测试,格式一致写错,不知道用postman该怎么测试了。所以花费了一点时间,记录下来,思考这个工具的执行过程。

Controller方法


3.参考连接

http://www.cnblogs.com/sandyliu1999/p/4802706.html



2018_05_24更新(6.0版本)

1、如果要传递多个参数其中一个Long类型的数组,另外一个是Long类型的数据,还有一个实体,那么后者可以直接传,前者的话用Body中的formData来进行传递就好了,具体如下图:

![](https://images2018.cnblogs.com/blog/1112483/201805/1112483-20180524171849503-376268906.png)

2、在上面提到传递实体接收的方式,我试了不行不过可以直接将实体属性按照表单那样去写就可以获取到了,如下图所示:

![](https://images2018.cnblogs.com/blog/1112483/201805/1112483-20180524175136404-560497635.png) 小技巧:可以直接复制JSON中的选项,然后将光标点击在“x-www-form-ulencoded”第一项最后Ctrl+V就可以直接粘贴了非常方便。
原文地址:https://www.cnblogs.com/jpfss/p/9082560.html