feign调用第三方接口服务

2023-10-30

前言

做个笔记,下次直接抄
这里需要拿到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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

feign调用第三方接口服务 的相关文章

随机推荐