!!!访问时请求网址注意匹配配置文件中的IP和端口
//获取商品信息
@RequestMapping(value = "/v/queryGoodsId",method = RequestMethod.POST)
public ReturnResult queryGoodsById(String id) throws Exception {
return localGoodsService.queryGoodsById(id);
}
注意:idea中方法的参数名和数据库中的可以不一样。(我这里写的一样)
/**
* 用户登录的方法
* @param phone
* @param password
* @param response
* @return map key:token value:token字符串
* @throws Exception
*/
@RequestMapping(value = "/doLogin",method = RequestMethod.POST)
public ReturnResult doLogin(String phone, String password, HttpServletResponse response)throws Exception{
return localUserService.validateToken(phone,password);
}
//抢购商品的方法
//之前方式写成了get,还按照post的方法去传参,没有拼接问号,导致token和goodsId报错为null。
@RequestMapping(value = "/v/getGoods",method = RequestMethod.POST)
public ReturnResult getGoods(String token,String goodsId) throws Exception {
return localGoodsService.getGoods(token,goodsId);
}
首先,同第三种情况的图一,要先拿到token,才能做这里的拼接哦。
@RequestMapping(value = "/v/getGoods",method = RequestMethod.GET)
public ReturnResult getGoods(String token,String goodsId) throws Exception {
return localGoodsService.getGoods(token,goodsId);
}
GET请求还可以不拼接:
点击Params,输入参数及value,可输入多个,即时显示在URL链接上。
@RequestMapping(value = "/add",method = RequestMethod.POST,produces = "application/json")
@ResponseBody
public Dto add(@RequestBody AddBlogCommentVo vo){
}
//HttpServletRequest request这个参数用来获取token (对应网页Network-XHR-Headers-Request-token)
@RequestMapping(value = "/logout", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Dto logout(HttpServletRequest request){
//验证token
String token = request.getHeader("token");
if (!tokenService.validate(request.getHeader("user-agent"),token)){
return DtoUtil.returnFail("token无效", ErrorCode.AUTH_TOKEN_INVALID);
}else {
try {
tokenService.delete(token);
return DtoUtil.returnDataSuccess("注销成功啦");
}catch (Exception e){
e.printStackTrace();
return DtoUtil.returnFail("注销失败",ErrorCode.AUTH_UNKNOWN);
}
}
}
@RequestMapping(value = "/validatephone", method = RequestMethod.PUT, produces = "application/json")
@ResponseBody
public Dto validatephone(@RequestParam("user") String userCode,
@RequestParam("code") String code) {
try {
if (localItripUserService.validatePhone(userCode, code)) {
return DtoUtil.returnSuccess("验证成功");
} else {
return DtoUtil.returnSuccess("验证失败");
}
} catch (Exception e) {
e.printStackTrace();
return DtoUtil.returnFail(e.getMessage(), ErrorCode.AUTH_UNKNOWN);
}
}
@RequestMapping(value = "/queryuserlinkuser", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public Dto<ItripUserLinkUser> queryuserlinkuser(@RequestBody ItripSearchUserLinkUserVO vo, HttpServletRequest request) {
List<ItripUserLinkUser> linkUserList = null;
String token = request.getHeader("token");
......
}
注意:当接口传参中出现HttpServletRequest request时,在postman测试时要,要在头部Headers里面传参
@RequestMapping(value = "/deluserlinkuser", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Dto deluserlinkuser(@RequestParam("ids") Long[] ids, HttpServletRequest request) {
String token = request.getHeader("token");
......
}
@RequestMapping(value = "/queryhotelfeature", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Dto<ItripLabelDicVo> queryhotelfeature() {
List<ItripLabelDic> itripLabelDics = null;
List<ItripLabelDicVo> itripLabelDicVos = null;
try {
Map param = new HashMap();
param.put("parentId", 16);
itripLabelDics = itripLabelDicService.getListByMap(param);
if (EmptyUtils.isNotEmpty(itripLabelDics)) {
itripLabelDicVos = new ArrayList<>();
for (ItripLabelDic dic : itripLabelDics) {
ItripLabelDicVo dicVo = new ItripLabelDicVo();
BeanUtils.copyProperties(dic, dicVo);
itripLabelDicVos.add(dicVo);
}
}
} catch (Exception e) {
e.printStackTrace();
return DtoUtil.returnFail("系统异常,获取失败", "10205");
}
return DtoUtil.returnSuccess("true", itripLabelDicVos);
}
和get方式请求传参基本一致,get方式传参可以在网址后面直接拼接?和数据内容,也可以把参数写在Params中。
@RequestMapping(value = "/deluserlinkuser", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Dto deluserlinkuser(@RequestParam("ids") Long[] ids, HttpServletRequest request) {
String token = request.getHeader("token");
try {
if (EmptyUtils.isEmpty(redisAPI.exist(token))) {
return DtoUtil.returnFail("token失效,请重登录", "100000");
} else {
if (ids.length == 1) {
if (EmptyUtils.isNotEmpty(itripOrderLinkUserService.getListByLinkedUserId(ids[0]))) {
return DtoUtil.returnFail("所选的常用联系人中有与某条待支付的订单关联的项,无法删除", "100431");
}else {
itripUserLinkUserService.removeById(ids[0]);
return DtoUtil.returnSuccess("true:删除成功");
}
}else if (ids.length>1){
for (Long id : ids) {
if (EmptyUtils.isNotEmpty(itripOrderLinkUserService.getListByLinkedUserId(id))){
return DtoUtil.returnFail("所选的常用联系人中有与某条待支付的订单关联的项,无法删除", "100431");
}else {
itripUserLinkUserService.removeById(id);
return DtoUtil.returnSuccess("true:删除成功");
}
}
}else {
return DtoUtil.returnFail("删除常用联系人失败","100432");
}
}
} catch (Exception e) {
e.printStackTrace();
return DtoUtil.returnFail("请选择要删除的常用联系人","100433");
}
return DtoUtil.returnFail("系统异常","false");
}
key是可以选择类型的,有text(文本)和File(文件)2种。
@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = "application/json")
@ResponseBody
public Dto upload(@RequestParam MultipartFile file, HttpServletRequest request) {}
@RequestMapping(value = "/delpic",method = RequestMethod.POST,produces = "application/json")
@ResponseBody
public Dto delpic(String imgName, HttpServletRequest request){}
【Body】
none:无请求主体
form-data:mutipart/form-data是网页表单用来传输数据的默认格式。可以模拟填写表单,并且提交表单。
可以上传一个文件作为key的value提交(如上传文件)。但该文件不会作为历史保存,只能在每次需要发送请求的时候,重新添加文件。
urlencoded:该模式和表单模式容易混淆,urlencoded不能上传文件,key-value会写入URL,而form-data模式的key-value不明显写入URL,而是直接提交。
raw: 可以包含任何东西,所有填写的text都会随着请求发送。
binary:image、audio 、video等二进制文件,注意不能保存历史,需要每次选择文件,然后提交。
【Pre-request Script】
请求发送前,需要执行的操作,常用于修改环境变量、设置全局变量等。
【test】
提供了很多检查、判断方法,用于测试接口是否可用,响应头中会生成一个测试报告,给出test检测的功能,是否可用。
(【Body】部分介绍参考:https://www.jianshu.com/p/4d77d39d6cf9)