栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

dubbo调用远程(其他项目的)接口的工具类

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

dubbo调用远程(其他项目的)接口的工具类

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;
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/988952.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号