方案ajax

RestTemplate远程调用—复杂API极简使用(面向Map

2021-07-22  本文已影响0人  小胖学编程

一句代码能完成的事,为什么要写两句呢?

RestTemplate远程调用的API是比较复杂的,用习惯Fegin接口后,在用RestTemplate感觉无从下手,那么如何使得RestTemplate可以极简使用呢?

1. Get请求调用

构建Get请求地址,格式uri?参数&参数

        URI url = UriComponentsBuilder
                .fromHttpUrl(请求路径)
                .queryParam("key", "value")
                .build().toUri();

构建header对象

    private HttpEntity<String> getHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return new HttpEntity<>(null, headers);
    }

发送Get请求:

ResponseEntity<Resp> responseEntity = restTemplate.exchange(url, HttpMethod.GET, header, Resp.class);

整体流程

1. URI url = UriComponentsBuilder
                .fromHttpUrl(请求路径)
                .queryParam("key", "value")
                .build().toUri();
2. HttpEntity<String> header = getHeaders();
3. ResponseEntity<Resp> responseEntity = restTemplate.exchange(url, HttpMethod.GET, header, Resp.class);

    private HttpEntity<String> getHeaders() {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        return new HttpEntity<>(null, headers);
    }

2. 发送Post请求

postForEntity发送请求:

    public void test(Req req,String url) {
        //构建body+header
        HttpHeaders requestHeaders = new HttpHeaders();
        requestHeaders.set("key", "value");
        HttpEntity<Req> requestEntity = new HttpEntity<>(req, requestHeaders);

        //3. 远程调用
        ResponseEntity<Resp> responseEntity =
                restTemplate.postForEntity(url, requestEntity, Resp.class);
    }

3. 极简的工具类——面向Map编程

3.1 建造者模式快速创建Map

public class HeadExample {

    private HeadExample() {
    }

    public static HeadExample.Builder builder() {
        return new HeadExample.Builder();
    }


    public static class Builder {

        private Map<String, String> header;

        Builder() {
            header = new HashMap<>();
        }

        public HeadExample.Builder put(String key, String value) {
            header.put(key, value);
            return this;
        }

        public HeadExample.Builder putAll(Map<String, String> map) {
            header.putAll(map);
            return this;
        }

        public Map<String, String> build() {
            return header;
        }
    }
}

3.2 构建RestTemplate的工具类

不面向RestTemplate的API,而是面向Map快速构建对象。

public class RestTemplateUtils {

    /**
     * 为request设置请求header
     *
     * @param body    请求的body
     * @param headers 请求的headers
     * @param <T>
     * @return body+header的集合
     */
    public static <T> HttpEntity<T> buildBodyAndHeader(T body, Map<String, String> headers) {
        HttpHeaders requestHeaders = new HttpHeaders();
        headers.forEach(requestHeaders::set);
        return new HttpEntity<>(body, requestHeaders);
    }

    /**
     * 构建get请求,格式:http://域名?参数&参数
     *
     * @param url    请求地址
     * @param params get的参数
     * @return 拼接后的请求地址
     */
    public static URI buildGetUrl(String url, Map<String, String> params) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder
                .fromHttpUrl(url);
        params.forEach(uriComponentsBuilder::queryParam);
        return uriComponentsBuilder.build().toUri();
    }

    /**
     * 仅构建header请求
     *
     * @param headers header的数据
     * @return header数据
     */
    public static HttpEntity buildHeader(Map<String, String> headers) {
        HttpHeaders requestHeaders = new HttpHeaders();
        headers.forEach(requestHeaders::set);
        return new HttpEntity<>(requestHeaders);
    }

}
上一篇下一篇

猜你喜欢

热点阅读