服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - 使用Spring RestTemplate 详解实践使用及拓展增强

使用Spring RestTemplate 详解实践使用及拓展增强

2022-03-04 00:35isyoungboy Java教程

这篇文章主要介绍了使用Spring RestTemplate 详解实践使用及拓展增强,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

RestTemplate 是什么?

RestTemplate 是Spring封装的一个Rest风格http请求框架,底层可以切换成HttpClient OkHttp 或者Netty实现,用户只需要关心RestTemplate怎么用而不需要关心底层框架如何操作,使用RestTemplate不需要关心如何手动转换返回的对象和到处都是的异常处理代码,可以让你的代码更简洁更优雅。

你可以在 spring-web 中找到它

主要类和接口

  • RestOperations 定义Rest 操作的接口
  • HttpAccessor 抽象http help 类
  • InterceptingHttpAccessor HttpAccess 装饰类拓展了拦截器功能
  • RestTemplate 具体实现类
  • ClientHttpRequestInterceptor 拦截器接口 用于拦截http请求
  • UriTemplateHandler uri模板处理器,后面拓展会用到

使用Spring RestTemplate 详解实践使用及拓展增强

基础使用

put delete 等方法参考get post 的写法

Get获取对象或对象集合

获取 Employee 集合

?
1
2
3
4
5
6
7
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();

返回对象list用exchange方法使用 ParameterizedTypeReference 指定返回类型 ,getForEntity 也可以使用 Object[].class 或 其他数组接收再转为List

获取单个对象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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 发送对象或集合

发送集合

?
1
2
3
4
5
6
7
8
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);

发送对象

?
1
2
3
4
5
6
7
8
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);

上传文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void uploadFile(){
    HttpHeaders headers = new HttpHeaders();
    //设置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中的一个类 参考

上传多个文件

在上传单个文件的基础上多加几个文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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参数必须写死的问题
  • 解决get*方法不好添加header信息的问题

继承RestTemplate 拓展get方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 继承RestTemplate 新加get* 方法 比原有的方法多了个 httpHeaders 参数
*/
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处理逻辑

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/**
* 根据uriTemplate 把 uriVariables 分成两类
* 一类是path params 一类是 query params 分开赋值
* 如 /xx/{id}/type  path params 就是 id uriVariables 剩下的就是query params 用?拼接在url后面
* 如果查询参数中有数组或集合类型的参数会转化成 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);
        //设置query param
        queryParam.forEach((k, v) -> {
            if (v instanceof Object[]) {
                Object[] arrayParam = (Object[]) v;
                //把数组类型的参数拼成 参数名 + [] 的形式 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();
        //设置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();
    }
}

实际使用

初始化RestTemplate

?
1
2
3
4
5
6
7
8
9
10
11
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setConnectTimeout(500);// 设置超时
requestFactory.setReadTimeout(500);
//new 自己定义的类
CustomerRestTemplate restTemplate = new CustomerRestTemplate();
//设置自定义的uri处理处理器
QueryParamsUrlTemplateHandler queryParamsUrlTemplateHandler = new QueryParamsUrlTemplateHandler();
//这里使用装饰模式 添加rootUri
RootUriTemplateHandler rootUriTemplateHandler = new RootUriTemplateHandler(outUrl, queryParamsUrlTemplateHandler);
restTemplate.setUriTemplateHandler(rootUriTemplateHandler);
restTemplate.setRequestFactory(requestFactory);

get请求示例

?
1
2
3
4
5
6
7
8
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

思考进一步封装

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

?
1
2
3
4
5
6
7
8
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();

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/isyoungboy/article/details/86608877

延伸 · 阅读

精彩推荐