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,将缩减业务范畴。