最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Spring RestTemplate 詳解實(shí)踐使用及拓展增強(qiáng)

 更新時(shí)間:2021年10月28日 10:28:54   作者:isyoungboy  
這篇文章主要介紹了使用Spring RestTemplate 詳解實(shí)踐使用及拓展增強(qiáng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

RestTemplate 是什么?

RestTemplate 是Spring封裝的一個(gè)Rest風(fēng)格http請(qǐng)求框架,底層可以切換成HttpClient OkHttp 或者Netty實(shí)現(xiàn),用戶只需要關(guān)心RestTemplate怎么用而不需要關(guān)心底層框架如何操作,使用RestTemplate不需要關(guān)心如何手動(dòng)轉(zhuǎn)換返回的對(duì)象和到處都是的異常處理代碼,可以讓你的代碼更簡(jiǎn)潔更優(yōu)雅。

你可以在 spring-web 中找到它

主要類和接口

  • RestOperations 定義Rest 操作的接口
  • HttpAccessor 抽象http help 類
  • InterceptingHttpAccessor HttpAccess 裝飾類拓展了攔截器功能
  • RestTemplate 具體實(shí)現(xiàn)類
  • ClientHttpRequestInterceptor 攔截器接口 用于攔截http請(qǐng)求
  • UriTemplateHandler uri模板處理器,后面拓展會(huì)用到

uml圖

基礎(chǔ)使用

put delete 等方法參考get post 的寫法

Get獲取對(duì)象或?qū)ο蠹?/h3>

獲取 Employee 集合

RestTemplate restTemplate = new RestTemplate();
ResponseEntity<List<Employee>> response = restTemplate.exchange(
  "http://localhost:8080/employees/",
  HttpMethod.GET,
  null,
  new ParameterizedTypeReference<List<Employee>>(){});
List<Employee> employees = response.getBody();

返回對(duì)象list用exchange方法使用 ParameterizedTypeReference 指定返回類型 ,getForEntity 也可以使用 Object[].class 或 其他數(shù)組接收再轉(zhuǎn)為L(zhǎng)ist

獲取單個(gè)對(duì)象

public class EmployeeList {
    private List<Employee> employees;
 
    public EmployeeList() {
        employees = new ArrayList<>();
    }
 
    // getter/setter
}
EmployeeList response = restTemplate.getForObject(
  "http://localhost:8080/employees",
  EmployeeList.class);
List<Employee> employees = response.getEmployees();

Post 發(fā)送對(duì)象或集合

發(fā)送集合

List<Employee> newEmployees = new ArrayList<>();
newEmployees.add(new Employee(3, "Intern"));
newEmployees.add(new Employee(4, "CEO"));
 
restTemplate.postForObject(
  "http://localhost:8080/employees/",
  newEmployees,
  ResponseEntity.class);

發(fā)送對(duì)象

List<Employee> newEmployees = new ArrayList<>();
newEmployees.add(new Employee(3, "Intern"));
newEmployees.add(new Employee(4, "CEO"));
 
restTemplate.postForObject(
  "http://localhost:8080/employees",
  new EmployeeList(newEmployees),
  ResponseEntity.class);

上傳文件

public void uploadFile(){
    HttpHeaders headers = new HttpHeaders();
    //設(shè)置Content-Type
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    MultiValueMap<String, Object> body
      = new LinkedMultiValueMap<>();
    body.add("file", getTestFile());
    HttpEntity<MultiValueMap<String, Object>> requestEntity
     = new HttpEntity<>(body, headers);
     
    String serverUrl = "http://localhost:8082/spring-rest/fileserver/singlefileupload/";
     
    RestTemplate restTemplate = new RestTemplate();
    ResponseEntity<String> response = restTemplate
      .postForEntity(serverUrl, requestEntity, String.class);
}
public FileSystemResource getTestFile(){
    return new FileSystemResource("./test.md")
}

FileSystemResource 是spring中的一個(gè)類 參考

上傳多個(gè)文件

在上傳單個(gè)文件的基礎(chǔ)上多加幾個(gè)文件

MultiValueMap<String, Object> body
  = new LinkedMultiValueMap<>();
body.add("files", getTestFile());
body.add("files", getTestFile());
body.add("files", getTestFile());
     
HttpEntity<MultiValueMap<String, Object>> requestEntity
  = new HttpEntity<>(body, headers);
 
String serverUrl = "http://localhost:8082/spring-rest/fileserver/multiplefileupload/";
 
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate
  .postForEntity(serverUrl, requestEntity, String.class);

Spring RestTemplate 拓展

  • 解決restTemplate get* url參數(shù)必須寫死的問題
  • 解決get*方法不好添加header信息的問題

繼承RestTemplate 拓展get方法

/**
* 繼承RestTemplate 新加get* 方法 比原有的方法多了個(gè) httpHeaders 參數(shù)
*/
public class CustomerRestTemplate extends RestTemplate {
    public <T> ResponseEntity<T> getForEntity(String url, HttpHeaders httpHeaders, Class<T> responseType, Object... uriVariables) throws RestClientException {
        HttpEntity<Object> requestEntity = new HttpEntity<>(httpHeaders);
        RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType);
        ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
        return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
    }
    public <T> ResponseEntity<T> getForEntity(String url, HttpHeaders httpHeaders, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
        HttpEntity<Object> requestEntity = new HttpEntity<>(httpHeaders);
        RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType);
        ResponseExtractor<ResponseEntity<T>> responseExtractor = responseEntityExtractor(responseType);
        return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
    }
    public <T> T getForObject(String url, HttpHeaders httpHeaders, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
        HttpEntity<Object> requestEntity = new HttpEntity<>(httpHeaders);
        RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType);
        ResponseExtractor<T> responseExtractor = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters());
        return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
    }
    public <T> T getForObject(String url, HttpHeaders httpHeaders, Class<T> responseType, Object... uriVariables) throws RestClientException {
        HttpEntity<Object> requestEntity = new HttpEntity<>(httpHeaders);
        RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType);
        ResponseExtractor<T> responseExtractor = new HttpMessageConverterExtractor<T>(responseType, getMessageConverters());
        return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables);
    }
}

拓展URI處理邏輯

/**
* 根據(jù)uriTemplate 把 uriVariables 分成兩類 
* 一類是path params 一類是 query params 分開賦值
* 如 /xx/{id}/type  path params 就是 id uriVariables 剩下的就是query params 用?拼接在url后面
* 如果查詢參數(shù)中有數(shù)組或集合類型的參數(shù)會(huì)轉(zhuǎn)化成 key[]=value1&key[]=value2...
*/
public class QueryParamsUrlTemplateHandler extends DefaultUriTemplateHandler {
    /**
     * 匹配path param
     */
    private static final Pattern NAMES_PATTERN = Pattern.compile("\\{([^/]+?)\\}");
    @Override
    public URI expand(String uriTemplate, Map<String, ?> uriVariables) {
        UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(uriTemplate);
        //解析uriTemplate 提取query param
        Map<String, ?> queryParam = getQueryParam(uriTemplate, uriVariables);
        //設(shè)置query param
        queryParam.forEach((k, v) -> {
            if (v instanceof Object[]) {
                Object[] arrayParam = (Object[]) v;
                //把數(shù)組類型的參數(shù)拼成 參數(shù)名 + [] 的形式 k[]  xx&kp[]=xx&k[]=xx
                String key = k + "[]";
                String strArrayParam = Stream.of(arrayParam).map(String::valueOf).collect(Collectors.joining("&" + key + "="));
                uriComponentsBuilder.queryParam(key, strArrayParam);
            } else if (v instanceof Iterable) {
                Iterable iterable = (Iterable) v;
                String key = k + "[]";
                String strArrayParam = Stream.of(iterable).map(String::valueOf).collect(Collectors.joining("&" + key + "="));
                uriComponentsBuilder.queryParam(key, strArrayParam);
            } else {
                uriComponentsBuilder.queryParam(k, v);
            }
        });
        uriTemplate = uriComponentsBuilder.build().toUriString();
        //設(shè)置path param
        return super.expand(uriTemplate, uriVariables);
    }
    /**
     * 解析uriTemplate 分離 query param
     *
     * @param uriTemplate  uri模板
     * @param uriVariables 全部的模板變量
     * @return 查詢變量
     */
    public Map<String, ?> getQueryParam(String uriTemplate, Map<String, ?> uriVariables) {
        if (uriTemplate == null) {
            return null;
        }
        if (uriTemplate.indexOf('{') == -1) {
            return uriVariables;
        }
        if (uriTemplate.indexOf(':') != -1) {
            uriTemplate = sanitizeSource(uriTemplate);
        }
        Map<String, Object> pathVariables = Maps.newHashMap();
        Matcher matcher = NAMES_PATTERN.matcher(uriTemplate);
        while (matcher.find()) {
            String matchKey = matcher.group(1);
            Object value = uriVariables.get(matchKey);
            if (value != null) {
                pathVariables.put(matchKey, value);
            }
        }
        //此處為了圖方便使用了 guava 工具包中的類 功能就是取差集
        MapDifference<String, Object> difference = Maps.difference(uriVariables, pathVariables);
        return difference.entriesOnlyOnLeft();
    }
    /**
     * Remove nested "{}" such as in URI vars with regular expressions.
     */
    private static String sanitizeSource(String source) {
        int level = 0;
        StringBuilder sb = new StringBuilder();
        for (char c : source.toCharArray()) {
            if (c == '{') {
                level++;
            }
            if (c == '}') {
                level--;
            }
            if (level > 1 || (level == 1 && c == '}')) {
                continue;
            }
            sb.append(c);
        }
        return sb.toString();
    }
}

實(shí)際使用

初始化RestTemplate

SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(500);// 設(shè)置超時(shí)
requestFactory.setReadTimeout(500);
//new 自己定義的類
CustomerRestTemplate restTemplate = new CustomerRestTemplate();
//設(shè)置自定義的uri處理處理器
QueryParamsUrlTemplateHandler queryParamsUrlTemplateHandler = new QueryParamsUrlTemplateHandler();
//這里使用裝飾模式 添加rootUri
RootUriTemplateHandler rootUriTemplateHandler = new RootUriTemplateHandler(outUrl, queryParamsUrlTemplateHandler);
restTemplate.setUriTemplateHandler(rootUriTemplateHandler);
restTemplate.setRequestFactory(requestFactory);

get請(qǐng)求示例

Map<String, Object> params = new HashMap<>();
params.put("id", "1");
params.put("param2", "2");
params.put("param", new Integer[]{1506, 1507});
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization", "Basic " + "your authorization");
ResponseEntity<Map[]> forEntity = restTemplate.getForEntity("/api/test/{id}", httpHeaders, Map[].class, params);
// url 為 api/test/1?param[]=1506&param[]=1507&param2=2

思考進(jìn)一步封裝

可以考慮使用建造者模式改造restTemplate

Employee employee = RestTemplate.build()
            .get("api/xxx/{id}")
            .header("xx","xx")
            .headers(new Headers())
            .param("xx","xx")
            .params(new HashMap(){{put("bb","bb");}})
            .targetClass(Employee.class)
            .execute();

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java普通類如何轉(zhuǎn)javafx程序

    java普通類如何轉(zhuǎn)javafx程序

    這篇文章主要介紹了java普通類如何轉(zhuǎn)javafx程序方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 詳解使用spring aop實(shí)現(xiàn)業(yè)務(wù)層mysql 讀寫分離

    詳解使用spring aop實(shí)現(xiàn)業(yè)務(wù)層mysql 讀寫分離

    本篇文章主要介紹了使用spring aop實(shí)現(xiàn)業(yè)務(wù)層mysql 讀寫分離,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-01-01
  • Java實(shí)現(xiàn)暴力匹配算法

    Java實(shí)現(xiàn)暴力匹配算法

    暴力匹配算法是一種簡(jiǎn)單的字符串匹配算法,本文主要介紹了Java實(shí)現(xiàn)暴力匹配算法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • springboot集成junit編寫單元測(cè)試實(shí)戰(zhàn)

    springboot集成junit編寫單元測(cè)試實(shí)戰(zhàn)

    在做單元測(cè)試時(shí),代碼覆蓋率常常被拿來作為衡量測(cè)試好壞的指標(biāo),本文主要介紹了springboot集成junit編寫單元測(cè)試實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • spring mvc 實(shí)現(xiàn)獲取后端傳遞的值操作示例

    spring mvc 實(shí)現(xiàn)獲取后端傳遞的值操作示例

    這篇文章主要介紹了spring mvc 實(shí)現(xiàn)獲取后端傳遞的值操作,結(jié)合實(shí)例形式詳細(xì)分析了spring mvc使用JSTL 方法獲取后端傳遞的值相關(guān)操作技巧
    2019-11-11
  • Java利用Socket類實(shí)現(xiàn)TCP通信程序

    Java利用Socket類實(shí)現(xiàn)TCP通信程序

    TCP通信能實(shí)現(xiàn)兩臺(tái)計(jì)算機(jī)之間的數(shù)據(jù)交互,通信的兩端,要嚴(yán)格區(qū)分為客戶端與服務(wù)端,下面我們就來看看Java如何利用Socket類實(shí)現(xiàn)TCP通信程序吧
    2024-02-02
  • 解決IDEA中多模塊下Mybatis逆向工程不生成相應(yīng)文件的情況

    解決IDEA中多模塊下Mybatis逆向工程不生成相應(yīng)文件的情況

    這篇文章主要介紹了解決IDEA中多模塊下Mybatis逆向工程不生成相應(yīng)文件的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01
  • SpringSecurity+JWT實(shí)現(xiàn)登錄流程分析

    SpringSecurity+JWT實(shí)現(xiàn)登錄流程分析

    Spring Security 是一個(gè)功能強(qiáng)大且高度可定制的身份驗(yàn)證和訪問控制框架,它是為Java應(yīng)用程序設(shè)計(jì)的,特別是那些基于Spring的應(yīng)用程序,下面給大家介紹SpringSecurity+JWT實(shí)現(xiàn)登錄流程,感興趣的朋友一起看看吧
    2024-12-12
  • java實(shí)現(xiàn)給圖片加鋪滿的網(wǎng)格式文字水印

    java實(shí)現(xiàn)給圖片加鋪滿的網(wǎng)格式文字水印

    這篇文章主要給大家介紹了關(guān)于java實(shí)現(xiàn)給圖片加鋪滿的網(wǎng)格式文字水印的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • 基于Spring boot @Value 注解注入屬性值的操作方法

    基于Spring boot @Value 注解注入屬性值的操作方法

    這篇文章主要介紹了結(jié)合SpEL使用@Value-基于配置文件或非配置的文件的值注入-Spring Boot的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-07-07

最新評(píng)論

新密市| 汽车| 邹平县| 将乐县| 宜黄县| 改则县| 高邑县| 青浦区| 襄樊市| 滁州市| 巴彦县| 正蓝旗| 安国市| 札达县| 河源市| 大关县| 崇仁县| 二手房| 武冈市| 永济市| 清镇市| 额济纳旗| 蓬安县| 巴彦淖尔市| 准格尔旗| 合阳县| 西华县| 怀宁县| 德阳市| 灵丘县| 北海市| 广昌县| 察雅县| 昌黎县| 松溪县| 梅州市| 柳林县| 沂水县| 天等县| 木里| 济源市|