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


一、单个参数传递


1.@RequestBody注解


   
   
  1. /**
  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. }

测试请求路径


2.@RequestParam

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


   
   
  1. /**
  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. }

请求路径:


3.@PathVariable注解

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

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


   
   
  1. /**
  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. }

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


二、传递pojo对象


1.@RequestBody注解


   
   
  1. /*测试添加实体*/
  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. }

postman通过json格式测试



2.直接写实体


   
   
  1. /*测试添加实体*/
  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. }

form表单测试

   
   
  1. <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>

postman测试格式


三、postman测试List类型参数

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

Controller方法


3.参考连接

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



            </div>
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐