99 lines
3.4 KiB
Java
99 lines
3.4 KiB
Java
|
|
package com.newyetai.sdk.chuanglan.util;
|
||
|
|
|
||
|
|
import com.newyetai.common.util.JsonUtils;
|
||
|
|
import com.newyetai.sdk.chuanglan.constant.ChuangLanConstants;
|
||
|
|
import com.newyetai.sdk.chuanglan.dto.ChuangLanResponseDTO;
|
||
|
|
import lombok.extern.slf4j.Slf4j;
|
||
|
|
import org.apache.commons.lang3.StringUtils;
|
||
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
||
|
|
import org.springframework.http.HttpEntity;
|
||
|
|
import org.springframework.http.HttpHeaders;
|
||
|
|
import org.springframework.http.MediaType;
|
||
|
|
import org.springframework.stereotype.Component;
|
||
|
|
import org.springframework.util.LinkedMultiValueMap;
|
||
|
|
import org.springframework.util.MultiValueMap;
|
||
|
|
import org.springframework.util.StopWatch;
|
||
|
|
import org.springframework.web.client.RestClientException;
|
||
|
|
import org.springframework.web.client.RestTemplate;
|
||
|
|
|
||
|
|
import java.util.LinkedHashMap;
|
||
|
|
import java.util.Map;
|
||
|
|
import java.util.Objects;
|
||
|
|
|
||
|
|
@Slf4j
|
||
|
|
@Component
|
||
|
|
public class HttpUtilCl<T, R> {
|
||
|
|
|
||
|
|
private static RestTemplate restTemplate;
|
||
|
|
|
||
|
|
@Autowired
|
||
|
|
public HttpUtilCl() {
|
||
|
|
this.restTemplate = new RestTemplate();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Http 通用请求封装
|
||
|
|
*
|
||
|
|
* @param url 请求地址
|
||
|
|
* @param requestDto 请求实体
|
||
|
|
* @param <T>
|
||
|
|
* @param <R>
|
||
|
|
* @return
|
||
|
|
*/
|
||
|
|
public static <T, R extends ChuangLanResponseDTO> ChuangLanResponseDTO execute(String url, T requestDto) {
|
||
|
|
ChuangLanResponseDTO result;
|
||
|
|
|
||
|
|
StopWatch stopWatch = new StopWatch();
|
||
|
|
stopWatch.start();
|
||
|
|
try {
|
||
|
|
String reqJson = JsonUtils.toJsonString(requestDto);
|
||
|
|
log.info("chuanglan request url :{}", url);
|
||
|
|
log.info("chuanglan request requestDto :{}", reqJson);
|
||
|
|
|
||
|
|
MultiValueMap requestBody = new LinkedMultiValueMap();
|
||
|
|
Map paramMap = JsonUtils.toMap(reqJson);
|
||
|
|
paramMap.forEach((k,v) -> {
|
||
|
|
if(Objects.nonNull(v)){
|
||
|
|
requestBody.add(k,v);
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
// 设置请求头
|
||
|
|
HttpHeaders headers = new HttpHeaders();
|
||
|
|
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
|
||
|
|
HttpEntity request = new HttpEntity(requestBody, headers);
|
||
|
|
String response = restTemplate.postForObject(url, request, String.class);
|
||
|
|
log.info("chuanglan 响应结果 :{}", response);
|
||
|
|
|
||
|
|
result = formatDto(response);
|
||
|
|
|
||
|
|
} catch (Exception e) {
|
||
|
|
log.error("chuanglan {} invoke error:{}", requestDto.getClass().getName(), e);
|
||
|
|
stopWatch.stop();
|
||
|
|
return ChuangLanResponseDTO.buildCommonError();
|
||
|
|
}
|
||
|
|
stopWatch.stop();
|
||
|
|
log.info("chuanglan consuming {} s", stopWatch.getTotalTimeSeconds());
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
private static ChuangLanResponseDTO formatDto(String json){
|
||
|
|
if(StringUtils.isBlank(json)){
|
||
|
|
return ChuangLanResponseDTO.buildCommonError();
|
||
|
|
}
|
||
|
|
ChuangLanResponseDTO result = JsonUtils.toObject(json,ChuangLanResponseDTO.class);
|
||
|
|
Map<String,Object> map = JsonUtils.toMap(json);
|
||
|
|
if(map.get("data") != null){
|
||
|
|
LinkedHashMap data = (LinkedHashMap) map.get("data");
|
||
|
|
// 创蓝接口验证失败
|
||
|
|
if(!ChuangLanConstants.IMPL_CODE.equals(data.get("result"))){
|
||
|
|
result.setMsg(data.get("remark") + "");
|
||
|
|
result.setMessage(data.get("remark") + "");
|
||
|
|
// result.setCode(data.get("result") + "");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|