zoukankan      html  css  js  c++  java
  • java 请求参数键值对重复问题

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

  • 相关阅读:
    3.2.5 复制节点:
    1.创建元素节点:
    有多少漏洞都会重来:从ElasticSearch到MongoDB和Redis
    登陆密码显示与不显示的切换
    计算机网络基础 — Linux 路由器
    作为白手起家的企业家,必经阶段你经历了几个?
    竞争越来越大,创业公司怎么才能走的越来越远?
    城市竞争太大?这有农村创业七大项目,让你远离竞争
    短视频的改革带来的风暴,网红又该何去何从?
    照着官方文档学习react
  • 原文地址:https://www.cnblogs.com/qqzhulu/p/10162807.html
Copyright © 2011-2022 走看看