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中获取用户IDString 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;
}