此代码包含普通get,post请求和下载,代码如下:
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
@Data
public class CustomButtonRequestDto {
@ApiModelProperty(value = "接口请求路径", required = true)
private String url;
@ApiModelProperty(value = "接口请求方式", required = true)
private String requestMethod;
@ApiModelProperty(value = "请求头信息")
private Map<String, String> headers;
@ApiModelProperty(value = "接口请求参数")
private Map<String, Object> parameters;
@ApiModelProperty(value = "get请求时必传:普通请求 DEFAULT,下载请求 DOWNLOAD")
private String interfaceType;
@ApiModelProperty(value = "post请求时 需要直接传集合时: LIST")
private String parameterPassingModeFlag;
}
@Slf4j
@Service
public class CustomButtonService {
@Autowired
private RestTemplate restTemplate;
public Object customButtonRequest(CustomButtonRequestDto customButtonRequestDto,
HttpServletRequest request) {
log.info("自定义按钮请求接口customButtonRequestDto:{}", JSONObject.toJSON(customButtonRequestDto));
String requestMethod = customButtonRequestDto.getRequestMethod();
if (StringUtils.isEmpty(requestMethod)) {
return null;
}
Map<String, String> header = customButtonRequestDto.getHeaders();
Map<String, Object> parameters = customButtonRequestDto.getParameters();
String url = customButtonRequestDto.getUrl();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Accept", "application/json");
headers.set("Authorization", request.getHeader("Authorization"));
headers.set("loginName", request.getHeader("loginName"));
headers.set("Brand",request.getHeader("Brand"));
switch (requestMethod) {
case "get":
setHeader(header, headers);
try {
StrBuilder strParam = StrBuilder.create();
String strUrlParam = "";
String urlParamPlaceholder = "";
if (MapUtil.isNotEmpty(parameters)) {
if (url.contains("?")) {
Iterator<Map.Entry<String, Object>> iterator = parameters.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
if (entry.getValue() instanceof Collection) {
urlParamPlaceholder = new Gson().toJson(entry.getValue());
strParam.append("&").append("selectedFields").append("=").append("{urlParamPlaceholder}");
} else {
strParam.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
}
strUrlParam = strParam.toString();
} else {
strParam.append("?");
Iterator<Map.Entry<String, Object>> iterator = parameters.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
if (entry.getValue() instanceof Collection) {
urlParamPlaceholder = new Gson().toJson(entry.getValue());
strParam.append("selectedFields").append("=").append("{urlParamPlaceholder}").append("&");
} else {
strParam.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
}
}
strUrlParam = StrUtil.removeSuffix(strParam, "&");
}
}
// 普通get请求接口
log.info("get请求接口开始");
if (StringUtils.isNotEmpty(customButtonRequestDto.getInterfaceType()) && "DEFAULT".equals(customButtonRequestDto.getInterfaceType())) {
HttpEntity<String> getHttpEntity = new HttpEntity<>(null, headers);
ResponseEntity<String> getResponse = restTemplate.exchange(url + strUrlParam, HttpMethod.GET, getHttpEntity, String.class, urlParamPlaceholder);
return getResponse.getBody();
}
// 下载get请求接口
if (StringUtils.isNotEmpty(customButtonRequestDto.getInterfaceType()) && "DOWNLOAD".equals(customButtonRequestDto.getInterfaceType())) {
HttpEntity<MultiValueMap<String, Object>> getHttpEntity = new HttpEntity<MultiValueMap<String, Object>>(null, headers);
ResponseEntity<byte[]> getResponse = restTemplate.exchange(url + strUrlParam, HttpMethod.GET, getHttpEntity, byte[].class);
return getResponse;
}
} catch (Exception e) {
log.info("异常信息:{}", e);
if(e instanceof HttpClientErrorException){
throw e;
}
return Response.buildFailure();
}
case "post":
setHeader(header, headers);
if (StringUtils.isNotEmpty(customButtonRequestDto.getInterfaceType()) && "DOWNLOAD".equals(customButtonRequestDto.getInterfaceType())) {
if (StringUtils.isNotBlank(customButtonRequestDto.getParameterPassingModeFlag()) && customButtonRequestDto.getParameterPassingModeFlag().equals("LIST")) {
HttpEntity postHttpEntity = new HttpEntity<>(MapUtil.isNotEmpty(parameters) ? parameters.get("selectedFields") : "", headers);
ResponseEntity<byte[]> responseEntity = restTemplate.exchange(url, HttpMethod.POST, postHttpEntity, byte[].class);
return responseEntity;
}
HttpEntity postHttpEntity = new HttpEntity<>(MapUtil.isNotEmpty(parameters) ? parameters : "", headers);
ResponseEntity<byte[]> responseEntity = restTemplate.exchange(url, HttpMethod.POST, postHttpEntity, byte[].class);
return responseEntity;
} else {
if (StringUtils.isNotBlank(customButtonRequestDto.getParameterPassingModeFlag()) && customButtonRequestDto.getParameterPassingModeFlag().equals("LIST")) {
HttpEntity postHttpEntity = new HttpEntity<>(MapUtil.isNotEmpty(parameters) ? parameters.get("selectedFields") : "", headers);
String jsonObject = restTemplate.postForObject(url, postHttpEntity, String.class);
return jsonObject;
}
HttpEntity postHttpEntity = new HttpEntity<>(MapUtil.isNotEmpty(parameters) ? parameters : "", headers);
String jsonObject = restTemplate.postForObject(url, postHttpEntity, String.class);
return jsonObject;
}
default:
return null;
}
}
private static void setHeader(Map<String, String> header, HttpHeaders headers) {
if (MapUtil.isNotEmpty(header)) {
Iterator<Map.Entry<String, String>> iterator = header.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> entry = iterator.next();
headers.set(entry.getKey(), entry.getValue());
}
}
}
}