feign调用第三方接口服务

前言

做个笔记,下次直接抄
这里需要拿到response的header做验签之类的操作
所以用feign.Response来接收响应

正文

第三方接口调用的feign,自测OK

import com.mea.pay.common.exception.BusinessException;
import com.mea.pay.tpsp.domain.dto.CardCreationRequestContentDTO;
import com.mea.pay.tpsp.domain.dto.CardListRequestContentDTO;
import com.mea.pay.tpsp.domain.dto.CardQueryRequestContentDTO;
import com.mea.pay.tpsp.domain.dto.TpspRequestDTO;
import feign.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;

/**
 * <pre>
 * 华为的IPG平台接口调用
 * // 自测用的url地址:http://127.0.0.1:8089   ${ipg.host}
 * </pre>
 * @author Heng.Wei
 * @date 2022-04-07 08:54:05
 */
@FeignClient(contextId = "tpspFeign", name = "tpspFeign", url = "${ipg.host}", fallbackFactory = TpspFallbackFactory.class)
public interface TpspFeign {

    /**
     * <pre>
     * 新建虚拟卡
     * </pre>
     *
     * @param tpspRequestDTO body参数对象
     * @param tpspHeaderDTO header参数对象
     * @return http response
     * @author Heng.Wei
     * @date 2022-04-08 08:53:59
     */
    @PostMapping("/customer/v1/card/create")
    feign.Response createCard(@RequestBody TpspRequestDTO<CardCreationRequestContentDTO> tpspRequestDTO, @RequestHeader HttpHeaders tpspHeaderDTO);

    /**
     * <pre>
     * 查询虚拟卡
     * </pre>
     *
     * @param tpspRequestDTO body参数对象
     * @param tpspHeaderDTO header参数对象
     * @return http response
     * @author Heng.Wei
     * @date 2022-04-08 08:53:59
     */
    @PostMapping("/customer/v1/card/query")
    feign.Response queryCard(@RequestBody TpspRequestDTO<CardQueryRequestContentDTO> tpspRequestDTO, @RequestHeader HttpHeaders tpspHeaderDTO);

    /**
     * <pre>
     * 查询虚拟卡列表
     * </pre>
     *
     * @param tpspRequestDTO body参数对象
     * @param tpspHeaderDTO header参数对象
     * @return http response
     * @author Heng.Wei
     * @date 2022-04-08 08:53:59
     */
    @PostMapping("/customer/v1/card/list")
    feign.Response listCard(@RequestBody TpspRequestDTO<CardListRequestContentDTO> tpspRequestDTO, @RequestHeader HttpHeaders tpspHeaderDTO);
}

@Component
@Slf4j
class TpspFallbackFactory implements FallbackFactory<TpspFeign> {

    @Override
    public TpspFeign create(Throwable cause) {

        log.error("Tpsp 接口调用失败:{}", cause.getMessage());
        return new TpspFeign() {

            @Override
            public Response createCard(TpspRequestDTO<CardCreationRequestContentDTO> tpspRequestDTO, HttpHeaders tpspHeaderDTO) {
                throw new BusinessException("createCard tpsp request failed");
            }

            @Override
            public Response queryCard(TpspRequestDTO<CardQueryRequestContentDTO> tpspRequestDTO, HttpHeaders tpspHeaderDTO) {
                throw new BusinessException("queryCard tpsp request failed");
            }

            @Override
            public Response listCard(TpspRequestDTO<CardListRequestContentDTO> tpspRequestDTO, HttpHeaders tpspHeaderDTO) {
                throw new BusinessException("listCard tpsp request failed");
            }
        };
    }

}

取responseBody 和 responseHeader

import com.alibaba.cloud.commons.io.IOUtils;
import feign.Response;

// 取responseBody 
Response.Body body = response.body();
String bodyStr = IOUtils.toString(body.asInputStream(), StandardCharsets.UTF_8);

// 取responseHeader
response.headers()
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值