Spring-RestTemplate多重泛型/多级泛型/多层泛型包装

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);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值