此文章在不涉及原理以及postman延展功能的条件下带您快速上手postman和controller层的自测
一、postman的header
1.1 cookie/session
1.1.1 浏览器端
session中的信息通常在登录的时候获取,为了让此博文信息清晰展示,所以此处密码没有进行加密
在login界面F12可以找到登录的cookie以及session信息,只要未超时或者服务器没有重新启动,那么session的值就不会变
1.1.2 postman中
点击右侧cookie,url的值一定要正确,然后add一个,将刚才的JSESSIONID复制到value值中,注意即使传参列表中有session,也不必写在参数列表中,因为在此步已经获取了。
1.1.3 参考资料
1.2 token
如果是token属性,发送完token请求后在postman中做如下处理:
二、POST请求
2.1 @RequestParam 注解
2.1.1 controller层部分:
@RequestMapping("/login")
public JsonResult login(@RequestParam String username, @RequestParam String password,
HttpSession session) {
User data = iUserService.login(username, password);
session.setAttribute("uid", data.getUid());
session.setAttribute("username", data.getUsername());
return JsonResult.success(data);
}
注意session前面不需要加@RequestParam注解,已经填写在cookie中了
2.1.2 postman部分
2.2 @RequestBody 注解
2.2.1 controller部分:
<!--pom.xml中引入repository--!>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
import com.alibaba.fastjson.JsonObject
@RequestMapping("/login")
public JsonResult login(@RequestBody JsonObject jsonObject,HttpSession session) {
//注意此处取值方式
String username=jsonObject.getString("username");
String password=jsonObject.getString("password");
//如果是其他类型,比如int可以强制转换 例如假设有一个id是int类型
// Integer id = Integer.valueOf(json.getString("id"));
User data = iUserService.login(username, password);
session.setAttribute("uid", data.getUid());
session.setAttribute("username", data.getUsername());
return JsonResult.success(data);
}
2.2.2 postman部分:
2.3 对象的传递
传递对象的时候一定要添加@RequestBody注解类
2.3.1 controller部分:
这个时候传递的是一个cartVO对象,实体类如下:
public class CartVO implements Serializable {
private Integer uid;
private Integer pageSize;
}
@RequestMapping({"/testpage"})
public JsonResult getVOByVo(@RequestBody CartVO cartVO){
return iCartService.getVoByVo(cartVO);
}
2.3.2 postman部分:
2.4 含文件类型作为参数
这时候没法使用常规post方法中的raw部分进行数据传输
2.4.1 只有文件的传输
2.4.1.1 单文件controller部分:
使用 (@RequestParam(value = "xxx")
注解类
@RequestMapping("/importProductExcel")
public JsonResult importProductExcel(@RequestParam(value = "file")MultipartFile file){
return iProduceService.importProduct(file);
}
2.4.1.2 单文件postman部分:
这时候form-data中的key需要和controller这边@RequestParam注解类中规定的value相同
2.4.1.3 多文件controller部分:
多文件时候将files以MultipartFile数组形式进行传递
@RequestMapping("/importProductExcel")
public JsonResult importProductExcel(@RequestParam(value = "files")MultipartFile[] files){
//xxxx
}
2.4.1.4 多文件postman部分:
仍然是在form-data部分填充参数,和单文件传输的格式一样,注意和@RequestParam中的对应。其中画红框的部分属性选择为file
2.4.2 其他+文件传输
如果这时候有别的参数也同样使用form-data,输入完参数后将类型选为text
2.4.2.1 controller部分
@PostMapping("/add")
public JsonResult saveEmoji(@RequestParam(value = "params") String jsString,@RequestParam(value="files") MultipartFile[] multipartFile){
//Controller层内容
}
2.4.2.2 postman部分:
其中非文件部分使用text
三、GET请求
3.1 请求格式
get请求的格式为:url?param1=xxx¶m2=yyy¶m3=
java中写法:
public JsonResult getUrlData(String param1){
String url="www.baidu.com"+"?"+"param1+"+param1+"param2=yyy¶m3="
}
get中允许拼接过程中参数为空的情况。如果是空字符串只可以传""
,不可以传null
(也就是这个参数为空的情况),也不可以传" "
,否则会出现param1=null
的问题,与param1=
是完全不同的情况。
postman中拼接情况如下,可以通过postman中get请求的url看java这边拼接的是否正确
3.2 @RequestParam 注解
上部分简述了如果涉及到参数拼接处理的情况,这部分简述在使用spring类框架过程中的处理情况
因为get请求参数只能直接拼接,所以使用@RequestParam 注解
3.2.1 controller部分
@GetMapping({"", "/"})
public JsonResult<List<District>> getByParent(@RequestParam String parent) {
List<District> data = districtService.getByParent(parent);
return new JsonResult<>(OK, data);
}
3.2.2 postman部分
3.3 对象的传递
3.3.1 controller部分
@GetMapping({"", "/"})
//注意:这里没有注解!直接传对象
public JsonResult<List<District>> getByParent(District dic) {
String parent=dic.getParent;
List<District> data = districtService.getByParent(parent);
return new JsonResult<>(OK, data);
}
3.3.2 postman部分
写法和添加@RequestParam时的请求方式是相同的,虽然现在在传递对象,但是仍然是在param的参数列表这边发起请求。
注意 如果是以对象的形式传递,如果该属性值为空,默认为null,如果需要的是空字符串需要转换下。