【springmvc】传值的几种方式&&postman接口测试
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.youkuaiyun.com/u013034640/article/details/76980779 </div>
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-3019150162.css">
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-3019150162.css">
<div class="htmledit_views" id="content_views">
最近在用postman测试postman接口,对于springmvc传值这一块,测试了几种常用方式,总结一下。对于postman这个工具的使用也增加了了解。postman测试很棒,有了工具,测试接口,事倍功半。
一、单个参数传递
1.@RequestBody注解
-
/**
-
* 测试单个参数@RequestBody
-
*/
-
@CrossOrigin
-
@RequestMapping(value = {"/insertTestParamsRequest"}, method = RequestMethod.GET)
-
@ResponseBody
-
public void insertTestParamsRequest(
@RequestBody String name,
@RequestBody String age) {
-
System.
out.println(
"name=====" + name);
-
System.
out.println(
"age=====" + age);
-
}
-
测试请求路径
2.@RequestParam
常用来处理简单类型的绑定,通过Request.getParameter() 获取的String可直接转换为简单类型的情况( ;
该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;
-
/**
-
* 测试单个参数@RequestParam
-
*/
-
@CrossOrigin
-
@RequestMapping(value = {"/insertTestParams"}, method = RequestMethod.GET)
-
@ResponseBody
-
public void insertTestParams(HttpServletRequest request,
@RequestParam String name,
@RequestParam String age) {
-
System.
out.println(
"name=====" + name);
-
System.
out.println(
"age=====" + age);
-
}
请求路径:
3.@PathVariable注解
路径为resultful风格,将参数当做请求路径。
当使用@RequestMapping URI template 样式映射时, 即 Url/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上。
-
/**
-
* 测试单个参数@PathVariable
-
*/
-
@CrossOrigin
-
@RequestMapping(value = {"/insertTest/{name}/{age}"}, method = RequestMethod.GET)
-
@ResponseBody
-
public void insertTestPathVeriable(HttpServletRequest request,
@PathVariable("name") String name,
@PathVariable String age) {
-
System.
out.println(
"name=====" + name);
-
System.
out.println(
"age=====" + age);
-
}
上面代码把URI template 中变量 name的值和age的值,绑定到方法的参数上。若方法参数名称和需要绑定的uri template中变量名称不一致,需要在@PathVariable("name")指定uri template中的名称。
二、传递pojo对象
1.@RequestBody注解
-
/*测试添加实体*/
-
@CrossOrigin
-
@RequestMapping(value = {"/insertEntityTest"}, method = RequestMethod.POST)
-
@ResponseBody
-
public void insertEntityTest(
@RequestBody CurriculumScheduleEntity curriculumScheduleEntity) {
-
System.
out.println(
"name=====" + curriculumScheduleEntity.getClassId());
-
System.
out.println(
"age=====" + curriculumScheduleEntity.getTeachclassId());
-
}
postman通过json格式测试
2.直接写实体
-
/*测试添加实体*/
-
@CrossOrigin
-
@RequestMapping(value = {"/insertTest"}, method = RequestMethod.POST)
-
@ResponseBody
-
public void insertTest(CurriculumScheduleEntity curriculumScheduleEntity) {
-
System.
out.println(
"name=====" + curriculumScheduleEntity.getClassId());
-
System.
out.println(
"age=====" + curriculumScheduleEntity.getWeekId());
-
}
form表单测试
-
<div>
-
<form action="/curriculumSchedule/insertTest" method="post">
-
classId :
<input name="classId">
<br>
-
teachClassId:
<input name="weekId">
<br>
-
<input type="submit" value="提交">
-
</form>
-
</div>
postman测试格式
三、postman测试List类型参数
以List<string>为例,测试批量删除方法,参数为List<String>。写这个其实没有什么技术,但是中午在测试List接口的时候,用postman测试,格式一致写错,不知道用postman该怎么测试了。所以花费了一点时间,记录下来,思考这个工具的执行过程。
Controller方法
3.参考连接
http://www.cnblogs.com/sandyliu1999/p/4802706.html