package com.suorui.dubbo.service.wechat.utils;
import com.alibaba.fastjson.JSONObject;
import com.suorui.dubbo.service.wechat.vo.HttpClientReqVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.sr.framework.boot.autoconfigure.httpclient.protocol.HttpClientBuilder;
import java.util.HashMap;
import java.util.Map;
@Component
@Slf4j
public class EsHttpClientUtil {
@Value("${findServiceVolume}")
private String findServiceVolume;
public String httpClient(HttpClientReqVo httpClientReqVo) {
String obj = "";
try {
obj = HttpClientBuilder.create().data(JSONObject.toJSONString(httpClientReqVo.getParam()))
.url(httpClientReqVo.getUrl())
.method(httpClientReqVo.getMethod())
.headers(httpClientReqVo.getHeaders())
.charset(httpClientReqVo.getCharset())
.connectTimeOut(2000)
.readTimeOut(2000)
.build();
} catch (Exception e) {
throw new RuntimeException("[HttpClientUtil]-httpClient 发送请求 异常", e);
}
return obj;
}
public HttpClientReqVo getServiceVolume(){
//拼接查询路径
String methodUrl = "/volume/num";
HttpClientReqVo reqVo = new HttpClientReqVo();
reqVo.setUrl(findServiceVolume + methodUrl);
reqVo.setMethod("GET");
reqVo.setCharset("UTF-8");
// log.info("[HttpClientUtil]-getFindIsDownHuanGoVo 获取查询数据入参对象:{}", JSONObject.toJSONString(reqVo));
return reqVo;
}
public HttpClientReqVo getLastYearServiceVolume(){
//拼接查询路径
String methodUrl = "/lastyearvolume/num";
HttpClientReqVo reqVo = new HttpClientReqVo();
reqVo.setUrl(findServiceVolume + methodUrl);
reqVo.setMethod("GET");
reqVo.setCharset("UTF-8");
// log.info("[HttpClientUtil]-getFindIsDownHuanGoVo 获取查询数据入参对象:{}", JSONObject.toJSONString(reqVo));
return reqVo;
}
}
package com.suorui.framework.boot.autoconfigure.httpclient.protocol;
import com.alibaba.fastjson.JSON;
import com.suorui.framework.boot.autoconfigure.httpclient.annotation.http.BodyParam;
import com.suorui.framework.boot.autoconfigure.httpclient.annotation.http.GetParam;
import com.suorui.framework.boot.autoconfigure.httpclient.annotation.http.PostParam;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@ToString
@Getter
public class HttpClientBuilder {
private T data;
private Map headers;
private String url;
private String method = "post";
private String charset="UTF-8";
private int connectTimeOut=2000;
private int readTimeOut=3000;
private Map params;
private String body;
public static HttpClientBuilder create() {
HttpClientBuilder o = new HttpClientBuilder<>();
return o;
}
public HttpClientBuilder params(Map params) {
this.params = params;
return this;
}
public HttpClientBuilder data(T data) {
this.data = data;
return this;
}
public HttpClientBuilder url(String url) {
this.url = url;
return this;
}
public HttpClientBuilder body(String body) {
this.body = body;
return this;
}
public HttpClientBuilder connectTimeOut(int connectTimeOut) {
this.connectTimeOut = connectTimeOut;
return this;
}
public HttpClientBuilder readTimeOut(int readTimeOut) {
this.readTimeOut = readTimeOut;
return this;
}
public HttpClientBuilder charset(String charset) {
this.charset = charset;
return this;
}
public HttpClientBuilder method(String method) {
this.method = method;
return this;
}
public HttpClientBuilder headers(Map headers) {
this.headers = headers;
return this;
}
public HttpClientBuilder addHeader(String key, String value) {
this.headers.put(key, value);
return this;
}
public HttpClientBuilder addHeaders(Map headers) {
this.headers.putAll(headers);
return this;
}
public String build() throws Exception {
try {
if(this.data!=null) {
if ("post".equals(this.method.trim().toLowerCase())) {
if (this.data instanceof String) {
this.body = (String) data;
}else {
addUrl(this.getValues(GetParam.class));
if(MapUtils.isEmpty(this.params)){
this.params= this.getValues(PostParam.class);
}else{
this.params.putAll(this.getValues(PostParam.class));
}
Map bodyMap = this.getValues(BodyParam.class);
if (!MapUtils.isEmpty(bodyMap)) {
this.body = JSON.toJSONString(bodyMap);
}
}
return HttpRequestUtil.post(this);
} else {
if (this.data instanceof String) {
this.body = (String) data;
}else {
if(MapUtils.isEmpty(this.params)){
this.params= this.getValues(GetParam.class);
}else{
this.params.putAll(this.getValues(GetParam.class));
}
addUrl(this.params);
}
return HttpRequestUtil.get(this);
}
}else{
if ("post".equals(this.method.trim().toLowerCase())) {
return HttpRequestUtil.post(this);
}else{
return HttpRequestUtil.get(this);
}
}
} catch (Exception e) {
throw e;
}
}
private void addUrl(Map getParams) {
if (!MapUtils.isEmpty(getParams)) {
if (this.url.indexOf("?") == -1) {
this.url=this.url.concat("?");
}else{
this.url=this.url.concat("&");
}
getParams.forEach((k, v) -> {
this.url = this.url.concat(k).concat("=").concat(v.toString()).concat("&");
});
this.url = this.url.substring(0, this.url.length() - 1);
}
}
private Map getValues(Class anno) throws IllegalAccessException {
Field[] declaredFields = this.data.getClass().getDeclaredFields();//该类声明的所有字段
Map map = new HashMap<>();
if (declaredFields != null && declaredFields.length > 0) {
for (Field field : declaredFields) {
if (field.isAnnotationPresent(anno)) {
field.setAccessible(true);
if (field.get(this.data) != null) {
map.put(field.getName(), field.get(this.data));
}
}
}
}
return map;
}
}