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

}

猜你喜欢

转载自blog.csdn.net/gracexiao168/article/details/129079124
今日推荐