使用Spring RestTemplate 详解实践使用及拓展增强
短信预约 -IT技能 免费直播动态提醒
RestTemplate 是什么?
RestTemplate 是Spring封装的一个Rest风格http请求框架,底层可以切换成HttpClient OkHttp 或者Netty实现,用户只需要关心RestTemplate怎么用而不需要关心底层框架如何操作,使用RestTemplate不需要关心如何手动转换返回的对象和到处都是的异常处理代码,可以让你的代码更简洁更优雅。
你可以在 spring-web 中找到它
主要类和接口
RestOperations
定义Rest 操作的接口HttpAccessor
抽象http help 类InterceptingHttpAccessor HttpAccess
装饰类拓展了拦截器功能RestTemplate
具体实现类ClientHttpRequestInterceptor
拦截器接口 用于拦截http请求UriTemplateHandler uri
模板处理器,后面拓展会用到
基础使用
put delete 等方法参考get post 的写法
Get获取对象或对象集合
获取 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();
返回对象list用exchange方法使用 ParameterizedTypeReference 指定返回类型 ,getForEntity 也可以使用 Object[].class 或 其他数组接收再转为List
获取单个对象
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 发送对象或集合
发送集合
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);
发送对象
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();
//设置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中的一个类 参考
上传多个文件
在上传单个文件的基础上多加几个文件
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方法
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处理逻辑
public class QueryParamsUrlTemplateHandler extends DefaultUriTemplateHandler {
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);
}
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();
}
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();
//这里使用装饰模式 添加rootUri
RootUriTemplateHandler rootUriTemplateHandler = new RootUriTemplateHandler(outUrl, queryParamsUrlTemplateHandler);
restTemplate.setUriTemplateHandler(rootUriTemplateHandler);
restTemplate.setRequestFactory(requestFactory);
get请求示例
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¶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();
以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341