码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-05-24 15:18:42      阅读:482      评论:0      收藏:0      [点我收藏+]

标签:title   通过   man   pop   一点   name   spring   提交   批量删除   

    最近在用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



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

标签:title   通过   man   pop   一点   name   spring   提交   批量删除   

原文地址:https://www.cnblogs.com/jpfss/p/9082560.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!