关于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;
}
相关推荐
小马爱打代码24 分钟前
Spring Boot :使用 Spring Cache 注解方式集成 Redis
spring boot·redis·spring
东东5161 小时前
果园预售系统的设计与实现spingboot+vue
前端·javascript·vue.js·spring boot·个人开发
不光头强1 小时前
spring boot项目欢迎页设置方式
java·spring boot·后端
怪兽毕设1 小时前
基于SpringBoot的选课调查系统
java·vue.js·spring boot·后端·node.js·选课调查系统
学IT的周星星2 小时前
Spring Boot Web 开发实战:第二天,从零搭个“会卖萌”的小项目
spring boot·后端·tomcat
笨蛋不要掉眼泪2 小时前
Spring Boot + RedisTemplate 数据结构的基础操作
java·数据结构·spring boot·redis·wpf
编程彩机3 小时前
互联网大厂Java面试:从分布式事务到微服务优化的技术场景解读
java·spring boot·redis·微服务·面试·kafka·分布式事务
Moshow郑锴3 小时前
Spring Boot Data API 与 Redis 集成:KPI/图表/表格查询的缓存优化方案
spring boot·redis·缓存
好好研究3 小时前
SpringBoot整合SpringMVC
xml·java·spring boot·后端·mvc
千寻技术帮3 小时前
10386_基于SpringBoot的外卖点餐管理系统
java·spring boot·vue·外卖点餐