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

java 请求参数键值对重复问题

时间:2018-12-23 00:28:35      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:date   127.0.0.1   service   bsp   ons   sources   amp   upload   time   

java在通过url访问接口的过程中,url需传入参数,可将请求参数作为键、请求参数获取的值作为值。在传参过程中,若键出现重复,则会出现覆盖现象,值可出现重复现象。

如以下案例:

案例1:

controller层:

@RestController
@RequestMapping("api")
public class MemberController {

@RequestMapping(value = "v2/member/member/resources/update") // 修改我的资源
public Object resourcesUpdate(
@RequestParam String rName,
@RequestParam String rDesc,
@RequestParam String id,
@RequestParam(value = "Images", required = false) MultipartFile[] Images,
HttpServletRequest request) {
Member member = memberService.checkIdAndToken(request);
if (member == null) {
return new APIResult(1000, "登录失效");
}
MemberResources memberResources = memberResourcesService.getByIdAndMemberId(id, member.getId());
memberResources.setRname(rName);
memberResources.setrDesc(rDesc);
memberResources.setCreateTime(new Date());
if (Images != null && Images.length > 0) {
String images = "[";
if (Images != null && Images.length > 0) {
for (int i = 0; i < Images.length; i++) {
String imageUrl = imageUploadService.uploadFile(Images[i]);
if (i == 0) {
images += imageUrl;
continue;
} else {
images += "," + imageUrl;
}
}
}
images += "]";
// 保存图片集合
memberResources.setImgUrl(images);
}
MemberResources memberResources1 = memberResourcesService.save(memberResources);

return APIResult.createSuccess(memberResources1);
}

}

service层

public Member checkIdAndToken(HttpServletRequest request) {
String id = HttpRequestUtils.getHeaderByName(request, "id");
String token = HttpRequestUtils.getHeaderByName(request, "token");
if (id == null) {
id = request.getParameter("id");
}
if (token == null) {
token = request.getParameter("token");
}
return memberRepository.findByIdAndToken(id, token);
}

若要对该接口进行访问,访问路径为:127.0.0.1:8080/api/v2/member/member/resources/update? id=**&token=**&rName=**&rDesc&id=**&images=**

id出现重复,请求结果返回"登录失效",若要解决此问题,可对controller层的id进行重命名,如rid。

案例2:

controller层:

@RestController
@RequestMapping("api")
public class MemberController {

@RequestMapping(value = "v2/member/member/resources/list") // 查询我的资源
public Object resourcesList(@RequestParam String memberId,
@RequestParam(required = false, defaultValue = PageConstants.DEFAULT_PAGE_NUMBER) Integer page,
@RequestParam(required = false, defaultValue = PageConstants.DEFAULT_PAGE_SIZE) Integer size,
HttpServletRequest request) {
Member member = memberService.checkIdAndToken(request);
if (member == null) {
return new APIResult(1000, "登录失效");
}
if (memberId != null && !memberId.equals("")) {
} else {
memberId = member.getId();
}
List<MemberResources> memberResourcesList = memberResourcesService
.getListByMemberIdOrderByCreateTimeDesc(memberId, page, size);
List<MemberResourcesVO> memberResourcesVOList = new ArrayList<>();
for (MemberResources memberResources : memberResourcesList) {
MemberResourcesVO memberResourcesVO = new MemberResourcesVO();
memberResourcesVO.setId(memberResources.getId());
memberResourcesVO.setCreateTime(memberResources.getCreateTime());
memberResourcesVO.setMemberId(memberResources.getMemberId());
memberResourcesVO.setrDesc(memberResources.getrDesc());
memberResourcesVO.setrName(memberResources.getRname());
if (memberResources.getImgUrl() != null && !memberResources.getImgUrl().equals("")) {
String imgUrl = memberResources.getImgUrl();
imgUrl = imgUrl.substring(1, imgUrl.length() - 1);
if (imgUrl.length() > 0) {
String[] img = imgUrl.split(",");
memberResourcesVO.setImgUrl(img);
} else {
memberResourcesVO.setImgUrl(null);
}
}
memberResourcesVOList.add(memberResourcesVO);
}
return APIResult.createSuccess(memberResourcesVOList);
}

}

service层

public Member checkIdAndToken(HttpServletRequest request) {
String id = HttpRequestUtils.getHeaderByName(request, "id");
String token = HttpRequestUtils.getHeaderByName(request, "token");
if (id == null) {
id = request.getParameter("id");
}
if (token == null) {
token = request.getParameter("token");
}
return memberRepository.findByIdAndToken(id, token);
}

这里的请求参数memberId可以为其他用户的id,也可以为登录用户的ie,还可以为空,若只考虑memberId=id,将缩减业务范畴。

 

java 请求参数键值对重复问题

标签:date   127.0.0.1   service   bsp   ons   sources   amp   upload   time   

原文地址:https://www.cnblogs.com/qqzhulu/p/10162807.html

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