Java工具类之基于rpc调用第三方接口

方法比较全,关注博主不迷路~

import com.uav.common.exception.base.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.Map;

@Component
@Slf4j
public class HttpClient {

    @Autowired
    RestTemplate restTemplate;

    static final String AUTHORIZATION = "Authorization";

    static final String ERROR_MSG = " rpc调用异常!";

    public String httpGetByUrl(String  token, String url, Map<String, Object> param){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.add(AUTHORIZATION, token);
        HttpEntity<Object> httpRequest = new HttpEntity<>(httpHeaders);
        ResponseEntity<String> response = null;
        try {
            response = restTemplate.exchange(url + getParamString(param), HttpMethod.GET, httpRequest, String.class, new HashMap<>());
        } catch (Exception e) {
            log.error("HttpClient.httpGetByUrl.response.error:", e);
            throw new BaseException(url + ERROR_MSG);
        }
        return response.getBody();
    }

    public String httpPostByUrl(String  token, String url, Object requestObj){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.add(AUTHORIZATION, token);
        HttpEntity<Object> httpRequest = new HttpEntity<>(requestObj, httpHeaders);
        String response = null;
        try {
            response = restTemplate.postForObject(url, httpRequest, String.class);
        } catch (Exception e) {
            log.error("HttpClient.httpPost.response.error:", e);
            throw new BaseException(url + ERROR_MSG);
        }
        return response;
    }


    public String httpPutByUrl(String  token, String url, Object requestObj){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.add(AUTHORIZATION, token);
        HttpEntity<Object> httpRequest = new HttpEntity<>(httpHeaders);
        String response = null;
        try {
            ResponseEntity<String > res = restTemplate.exchange(url, HttpMethod.PUT, httpRequest, String .class,
                    requestObj);
            response = res.getBody();
        } catch (Exception e) {
            log.error("HttpClient.httpPut.response.error:", e);
            throw new BaseException(url + ERROR_MSG);
        }
        return response;
    }

    public String httpDeleteByUrl(String  token, String url, Object requestObj){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        httpHeaders.add(AUTHORIZATION, token);
        HttpEntity<Object> httpRequest = new HttpEntity<>(httpHeaders);
        String response = null;
        try {
            ResponseEntity<String > res = restTemplate.exchange(url, HttpMethod.DELETE, httpRequest, String .class,
                    requestObj);
            response =  res.getBody();
        } catch (Exception e) {
            log.error("HttpClient.httpDelete.response.error:", e);
            throw new BaseException(url + ERROR_MSG);
        }
        return response;
    }

    // 调用示例  注入本类 然后直接.method
    public static void main(String[] args) {
        String token = "";
        String url = "";
        Map<String, Object> map = new HashMap<>();
        map.put("name","0124");
        map.put("age","1");
        map.put("sex","1");
//        String a = httpClient.httpPostByUrl(token,url,map);

    }
    private String getParamString(Map<String, Object> param) {
        if (param == null) {
            return "";
        }
        StringBuilder paramBuilder = new StringBuilder();
        paramBuilder.append("?");
        for (Map.Entry<String, Object> item : param.entrySet()) {
            paramBuilder.append(item.getKey()).append("=").append(item.getValue())
                    .append("&");
        }
        String result = paramBuilder.toString();
        return result.substring(0, result.length() - 1);
    }

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr.杨先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值