Spring-RestTemplate多重泛型/多级泛型/多层泛型包装
在调用外部接口的时候、很多时候是是使用spring中的RestTemplate。
但是在返回的结果集是多级泛型的情况下、封装的就不是很完美了。
如下返回结果:
public Result<Page<ViewVO>> getAll(Param param){}
这样的返回结果若在使用他封装的getForObject
等方法,结果还是再次进行处理。
所以只能使用RestTemplate提供的exchange
方法、指定返回类型。具体使用查看示例代码
所以进行一个简单的包装:
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
/**
*
* @author 傲寒
* @date 2022/9/30
*/
@Slf4j
public class RestTemplateProvider {
private final RestTemplate restTemplate;
public RestTemplateProvider(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}
/**
* @param url url
* @param resultType 结果类型
* @param uriVariables uri变量
* @return {@link R}
*/
public <R> R getRest(String url, ParameterizedTypeReference<R> resultType, Object... uriVariables) {
return exchange(HttpMethod.GET, url, HttpEntity.EMPTY, resultType, uriVariables);
}
public <R> R getRest(String url, HttpEntity<?> httpEntity, ParameterizedTypeReference<R> resultType, Object... uriVariables) {
return exchange(HttpMethod.GET, url, httpEntity, resultType, uriVariables);
}
/**
* @param url url
* @param httpEntity http实体
* @param resultType 结果类型
* @param uriVariables uri变量
* @return {@link R}
*/
public <R> R postRest(String url, HttpEntity<?> httpEntity, ParameterizedTypeReference<R> resultType, Object... uriVariables) {
return exchange(HttpMethod.POST, url, httpEntity, resultType, uriVariables);
}
/**
* @param url url
* @param resultType 结果类型
* @param uriVariables uri变量
* @return {@link R}
*/
public <R> R postRest(String url, ParameterizedTypeReference<R> resultType, Object... uriVariables) {
return exchange(HttpMethod.POST, url, HttpEntity.EMPTY, resultType, uriVariables);
}
/**
* 交换
*
* @param url url
* @param httpMethod http方法
* @param res 返回结果类型
* @param uriVariables uri变量
* @param HttpEntity http实体
* @return {@link R}
*/
public <R> R exchange(HttpMethod httpMethod,
String url,
HttpEntity<?> HttpEntity,
ParameterizedTypeReference<R> res,
Object... uriVariables) {
log.info(" httpMethod:[{}], url:[{}], uriVariables:[{}]", httpMethod, url, uriVariables);
ResponseEntity<R> exchange;
try {
exchange = restTemplate.exchange(url, httpMethod, HttpEntity, res, uriVariables);
} catch (Exception e) {
log.warn("url:[{}], httpMethod:[{}], uriVariables:[{}]", url, httpMethod, uriVariables, e);
throw new RuntimeException(e);
}
return getRes(exchange);
}
/**
* 获得 返回结果
*
* @param exchange 交换
* @return {@link R}
*/
private <R> R getRes(ResponseEntity<R> exchange) {
HttpStatus statusCode = exchange.getStatusCode();
if (statusCode == HttpStatus.OK) {
return exchange.getBody();
} else {
log.error("statusCode:[{}], body:[{}]", statusCode.value(), exchange.getBody());
throw new RuntimeException();
}
}
}
示例代码:
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
RestTemplateProvider restTemplateUtils = new RestTemplateProvider(restTemplate);
String param = "傲寒";
//返回类型指定String 返回的json格式
String jsonStr = restTemplateUtils.getRest("https://localhost:8080/api/test/{param1}",
new ParameterizedTypeReference<String>() {}, param);
System.out.println(jsonStr);
//指定类型 Result<Page<ViewVO>>
Result<Page<ViewVO>> res = restTemplateUtils.getRest("https://localhost:8080/api/test/{param1}",
new ParameterizedTypeReference<Result<Page<ViewVO>>>() {}, param);
System.out.println(res);
}