关于RestController发送请求用List<T> 接收数据

1、发送get请求时,后端可用List<T> 来接收参数,前端或者其他请求可通过逗号 分割参数即可;同时注意特殊字符转换,如日期格式

接收方(后端):

复制代码
  @ApiOperation(value = "手动计算扩缩容", notes = "手动计算扩缩容")
    @GetMapping(value = "/resource/hand")
    public HttpResponse<Object> scaleResource(@RequestParam(name = "ips", required = false) @ApiParam(value = "ip") List<String> ips,
                                      @RequestParam(name = "businesses", required = false)  @ApiParam(value = "业务") List<String> businesses,
                                      @RequestParam(name = "startDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @ApiParam(value = "开始时间", format = "yyyy-MM-dd", example = "2024-01-01") LocalDate startDate,
                                      @RequestParam(name = "endDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) @ApiParam(value = "结束时间", format = "yyyy-MM-dd", example = "2024-01-01") LocalDate endDate,
                                      @RequestParam(name = "createdBy") @ApiParam(value = "操作人", required = true) String createdBy,
                                      @RequestParam(name = "resourceType", required = false) @ApiParam(value = "资源类型") String resourceType,
                                              @RequestParam(name = "description", required = false) @ApiParam(value = "描述") String description) {
        try{
            if(Objects.isNull(startDate) && Objects.nonNull(endDate)){
                return new HttpResponse<>().error(400, "结束时间不为空时,开始时间也不能为空");
            }
            if(Objects.nonNull(startDate) && Objects.isNull(endDate)){
                return new HttpResponse<>().error(400, "开始时间不为空时,结束时间也不能为空");
            }
            List<Scale> list = scaleService.scaleResource(ips, businesses, startDate, endDate, createdBy, resourceType, description);
            return new HttpResponse<>().success(list);
        }catch (Exception e){
            return new HttpResponse<>().error(500, e.getMessage());
        }
    }

调用方(页面):

复制代码
public String handScaleInfo(){
        logger.info("手动计算扩缩容操作开始...");
        // session中获取用户ID
        String userId = ((UserBean) ServletActionContext.getRequest()
                .getSession().getAttribute(SessionKeys.userInfo.toString()))
                .getUserId();
        if (userId == null || userId == "") {
            logger.error("创建人信息为空,手动扩缩容失败");
            errMsg = "创建人信息为空,手动扩缩容失败";
            return ConstantEnum.ERROR.toString();
        }
        handScale.setCreatedBy(userId);
        Integer pageNum = getPage();
        Integer pageSize = getPageSize();
        String urlParams = Util.getUrlParams(handScale, pageNum, pageSize,"startTime");
        String url = scaleUrl + "/scale/resource/hand/" + urlParams.replace(" ", "%20").replace(":", "%3A");;
        String response = Util.doGet(url);
        if (StringUtils.isNotEmpty(response)) {
            HttpResponse httpResponse = JSON.parseObject(response, HttpResponse.class);
            if (200 == httpResponse.getCode()) {
                result.setResultFlage(ConstantEnum.SUCCESS.toString());
                result.setResultMessage(getText("resourceAnalysis.opt.success"));
            } else {
                result.setResultFlage(ConstantEnum.FAILURE.toString());
                result.setResultMessage(getText("resourceAnalysis.http.fail"));
                return ConstantEnum.FAILURE.toString();
            }
        } else {
            result.setResultFlage(ConstantEnum.FAILURE.toString());
            result.setResultMessage(getText("resourceAnalysis.http.fail"));
            return ConstantEnum.FAILURE.toString();
        }

        return ConstantEnum.SUCCESS.toString();
    }

@Data
public class HandScale {

     /**
     * 多个参数逗号分割
     */
    private String businesses;
    private String ips;
    private String resourceType;
    private String description;
    private String startDate;
    private String endDate;
    private String createdBy;
}
相关推荐
cg50179 小时前
Spring Boot 的配置文件
java·linux·spring boot
橘猫云计算机设计13 小时前
基于springboot的考研成绩查询系统(源码+lw+部署文档+讲解),源码可白嫖!
java·spring boot·后端·python·考研·django·毕业设计
有一只柴犬13 小时前
深入Spring AI:6大核心概念带你入门AI开发
spring boot·后端
向阳25613 小时前
SpringBoot+vue前后端分离整合sa-token(无cookie登录态 & 详细的登录流程)
java·vue.js·spring boot·后端·sa-token·springboot·登录流程
XiaoLeisj13 小时前
【MyBatis】深入解析 MyBatis XML 开发:增删改查操作和方法命名规范、@Param 重命名参数、XML 返回自增主键方法
xml·java·数据库·spring boot·sql·intellij-idea·mybatis
风象南13 小时前
SpringBoot实现数据库读写分离的3种方案
java·spring boot·后端
CryptoPP14 小时前
springboot 对接马来西亚数据源API等多个国家的数据源
spring boot·后端·python·金融·区块链
清风絮柳14 小时前
52.个人健康管理系统小程序(基于springboot&vue)
vue.js·spring boot·毕业设计·前后端分离·健康管理系统·个人健康管理系统·个人健康管理小程序
forestsea15 小时前
使用 Spring Boot 和 GraalVM 的原生镜像
java·spring boot·spring native·原生映像
爱的叹息16 小时前
Spring Boot 集成Redis 的Lua脚本详解
spring boot·redis·lua