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

目录

  • RestTemplate 是什么?
  • 主要类和接口
  • 基础使用
    • Get获取对象或对象集合
    • Post 发送对象或集合
    • 上传文件
    • 上传多个文件
  • Spring RestTemplate 拓展
    • 继承RestTemplate 拓展get方法
    • 拓展URI处理逻辑
    • 实际使用
    • 思考进一步封装

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|使用Spring RestTemplate 详解实践使用及拓展增强
文章图片


基础使用 put delete 等方法参考get post 的写法

Get获取对象或对象集合
获取 Employee 集合
RestTemplate restTemplate = new RestTemplate(); ResponseEntity> response = restTemplate.exchange("http://localhost:8080/employees/",HttpMethod.GET,null,new ParameterizedTypeReference>(){}); List employees = response.getBody();

返回对象list用exchange方法使用 ParameterizedTypeReference 指定返回类型 ,getForEntity 也可以使用 Object[].class 或 其他数组接收再转为List
获取单个对象
public class EmployeeList {private List employees; public EmployeeList() {employees = new ArrayList<>(); } // getter/setter}EmployeeList response = restTemplate.getForObject("http://localhost:8080/employees",EmployeeList.class); List employees = response.getEmployees();

【使用Spring|使用Spring RestTemplate 详解实践使用及拓展增强】
Post 发送对象或集合
发送集合
List newEmployees = new ArrayList<>(); newEmployees.add(new Employee(3, "Intern")); newEmployees.add(new Employee(4, "CEO")); restTemplate.postForObject("http://localhost:8080/employees/",newEmployees,ResponseEntity.class);

发送对象
List 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(); //设置Content-Typeheaders.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap body= new LinkedMultiValueMap<>(); body.add("file", getTestFile()); HttpEntity requestEntity= new HttpEntity<>(body, headers); String serverUrl = "http://localhost:8082/spring-rest/fileserver/singlefileupload/"; RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = restTemplate.postForEntity(serverUrl, requestEntity, String.class); }public FileSystemResource getTestFile(){return new FileSystemResource("./test.md")}

FileSystemResource 是spring中的一个类 参考

上传多个文件
在上传单个文件的基础上多加几个文件
MultiValueMap body= new LinkedMultiValueMap<>(); body.add("files", getTestFile()); body.add("files", getTestFile()); body.add("files", getTestFile()); HttpEntity requestEntity= new HttpEntity<>(body, headers); String serverUrl = "http://localhost:8082/spring-rest/fileserver/multiplefileupload/"; RestTemplate restTemplate = new RestTemplate(); ResponseEntity response = restTemplate.postForEntity(serverUrl, requestEntity, String.class);


Spring RestTemplate 拓展
  • 解决restTemplate get* url参数必须写死的问题
  • 解决get*方法不好添加header信息的问题

继承RestTemplate 拓展get方法
/*** 继承RestTemplate 新加get* 方法 比原有的方法多了个 httpHeaders 参数*/public class CustomerRestTemplate extends RestTemplate {public ResponseEntity getForEntity(String url, HttpHeaders httpHeaders, Class responseType, Object... uriVariables) throws RestClientException {HttpEntity requestEntity = new HttpEntity<>(httpHeaders); RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType); ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables); }public ResponseEntity getForEntity(String url, HttpHeaders httpHeaders, Class responseType, Map uriVariables) throws RestClientException {HttpEntity requestEntity = new HttpEntity<>(httpHeaders); RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType); ResponseExtractor> responseExtractor = responseEntityExtractor(responseType); return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables); }public T getForObject(String url, HttpHeaders httpHeaders, Class responseType, Map uriVariables) throws RestClientException {HttpEntity requestEntity = new HttpEntity<>(httpHeaders); RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType); ResponseExtractor responseExtractor = new HttpMessageConverterExtractor(responseType, getMessageConverters()); return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables); }public T getForObject(String url, HttpHeaders httpHeaders, Class responseType, Object... uriVariables) throws RestClientException {HttpEntity requestEntity = new HttpEntity<>(httpHeaders); RequestCallback requestCallback = httpEntityCallback(requestEntity, responseType); ResponseExtractor responseExtractor = new HttpMessageConverterExtractor(responseType, getMessageConverters()); return execute(url, HttpMethod.GET, requestCallback, responseExtractor, uriVariables); }}

拓展URI处理逻辑
/*** 根据uriTemplate 把 uriVariables 分成两类 * 一类是path params 一类是 query params 分开赋值* 如 /xx/{id}/typepath 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("\\{([^/]+?)\\}"); @Overridepublic URI expand(String uriTemplate, Map uriVariables) {UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromHttpUrl(uriTemplate); //解析uriTemplate 提取query paramMap queryParam = getQueryParam(uriTemplate, uriVariables); //设置query paramqueryParam.forEach((k, v) -> {if (v instanceof Object[]) {Object[] arrayParam = (Object[]) v; //把数组类型的参数拼成 参数名 + [] 的形式 k[]xx&kp[]=xx&k[]=xxString 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 paramreturn super.expand(uriTemplate, uriVariables); }/*** 解析uriTemplate 分离 query param** @param uriTemplateuri模板* @param uriVariables 全部的模板变量* @return 查询变量*/public Map getQueryParam(String uriTemplate, Map uriVariables) {if (uriTemplate == null) {return null; }if (uriTemplate.indexOf('{') == -1) {return uriVariables; }if (uriTemplate.indexOf(':') != -1) {uriTemplate = sanitizeSource(uriTemplate); }Map pathVariables = Maps.newHashMap(); Matcher matcher = NAMES_PATTERN.matcher(uriTemplate); while (matcher.find()) {String matchKey = matcher.group(1); Object value = https://www.it610.com/article/uriVariables.get(matchKey); if (value != null) {pathVariables.put(matchKey, value); }}//此处为了图方便使用了 guava 工具包中的类 功能就是取差集MapDifference 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
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(500); // 设置超时requestFactory.setReadTimeout(500); //new 自己定义的类CustomerRestTemplate restTemplate = new CustomerRestTemplate(); //设置自定义的uri处理处理器QueryParamsUrlTemplateHandler queryParamsUrlTemplateHandler = new QueryParamsUrlTemplateHandler(); //这里使用装饰模式 添加rootUriRootUriTemplateHandler rootUriTemplateHandler = new RootUriTemplateHandler(outUrl, queryParamsUrlTemplateHandler); restTemplate.setUriTemplateHandler(rootUriTemplateHandler); restTemplate.setRequestFactory(requestFactory);

get请求示例
Map 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 forEntity = restTemplate.getForEntity("/api/test/{id}", httpHeaders, Map[].class, params); // url 为 api/test/1?param[]=1506¶m[]=1507¶m2=2


思考进一步封装
可以考虑使用建造者模式改造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();

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

    推荐阅读