http请求工具类
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.nancal.workflow.common.constant.HttpRequestConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@Slf4j
public class WfHttpRequestUtils {
public static final String RESPONSE_STATUS = "responseStatus";
public static final String RESPONSE_BODY = "responseBody";
/**
* 获取外部请求返回值
* @param address 请求地址
* @param requestType 请求类型
* @param requestParam 直接请求参数
* @param variables 额外参数
* @return
*/
public static String getResponseBody(String address, String requestType, String requestParam, Map<String, Object> variables) {
Map<String, Object> map = executeRequest(address, requestType, requestParam, variables);
String body = (String)map.get(RESPONSE_BODY);
return body;
}
public static Map<String, Object> getResponseMap(String address, String requestType, String requestParam, Map<String, Object> variables) {
Map<String, Object> map = executeRequest(address, requestType, requestParam, variables);
return map;
}
private static Map<String, Object> executeRequest(String address, String requestType, String requestParam, Map<String, Object> variables) {
JSONObject jsonObject = new JSONObject();
if (StrUtil.isNotBlank(requestParam)) {
JSONObject paramJson = JSONUtil.parseObj(requestParam);
Iterator<String> keyIter = paramJson.keySet().iterator();
while (keyIter.hasNext()) {
String key = keyIter.next();
Object value = paramJson.get(key);
jsonObject.putIfAbsent(key, value);
}
}
if (CollectionUtil.isNotEmpty(variables)) {
Iterator<String> keyIter = variables.keySet().iterator();
while (keyIter.hasNext()) {
String key = keyIter.next();
Object value = variables.get(key);
jsonObject.putIfAbsent(key, value);
}
}
String finalParams = JSONUtil.toJsonStr(jsonObject);
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
String token = null;
if (attributes != null) {
HttpServletRequest request = attributes.getRequest();
token = request.getHeader("token");
}
log.info("根据外部地址获取人员信息参数, 地址:{}, 类型: {}, 参数: {}, token: {}", address, requestType, finalParams, token);
HttpRequest httpRequest = null;
if (HttpRequestConstant.POST.equals(requestType)) {
httpRequest = HttpRequest.post(address);
} else if (HttpRequestConstant.GET.equals(requestType)) {
httpRequest = HttpRequest.get(address);
} else if (HttpRequestConstant.DELETE.equals(requestType)) {
httpRequest = HttpRequest.delete(address);
}
Map<String, Object> resultMap = new HashMap<>();
String responseBody = null;
Boolean responseStatus = false;
if (httpRequest != null) {
httpRequest.setFollowRedirects(true);
HttpResponse response = null;
try {
response = httpRequest.header("Content-Type", "application/json")
.header("token", token)
.body(finalParams)
.execute();
String body = response.body();
responseStatus = response.isOk();
if (responseStatus && StrUtil.isNotBlank(body)) {
log.info("外部接口返回: {}", body);
responseBody = body;
}
} catch (Exception ex) {
log.error("外部接口调用失败:{}", ex.getMessage());
ex.printStackTrace();
}
}
resultMap.put(RESPONSE_BODY, responseBody);
resultMap.put(RESPONSE_STATUS, responseStatus);
return resultMap;
}
}