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

  • 相关阅读:
    【知识整理】这可能是最好的性能优化教程(一)
    【工作感悟】Android 开发者,如何提升自己的职场竞争力?
    MySql 主从复制
    MyCat 介绍、分片规则、调优的内容收集
    MyCat 安装部署,实现数据库分片存储
    [转]Activemq管理和基本介绍
    [转]ActiveMQ 即时通讯服务 浅析
    Redis 3.0集群 Window搭建方案
    【转】史上最全的“大数据”学习资源整理
    【转】【漫画解读】HDFS存储原理
  • 原文地址:https://www.cnblogs.com/qqzhulu/p/10162807.html
Copyright © 2011-2022 走看看