欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

通过Feign远程调用第三方接口

程序员文章站 2022-06-27 11:14:39
...

远程调用第三方接口的有几种方法,常用就是用HttpClient等插件,其实SpringCloud的Fegin组件也可以 用于第三方接口调用。

1、引入Maven插件

        <!-- feign核心包 -->
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-core</artifactId>
            <version>10.4.0</version>
        </dependency>

2、编写Feign配置类,用于创建Fegin调用服务

import com.foresee.sznf.wxgzh.service.FeignYcqhActionService;
import feign.Feign;
import feign.Request;
import feign.Retryer;
import feign.httpclient.ApacheHttpClient;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author chenweifeng
 * @version 0.0.1
 * @date 2019/11/13 09:38
 * @since JDK 10
 */
@Slf4j
@Configuration
@Component
public class FeignConfig {

    private String baseUrl="xxxxxxxxxxxxx";

    /**
     * 初始化feign服务
     *
     * @return
     */
    @Bean
    public FeignYcqhActionService initFeignYcqhActionService() {

        FeignYcqhActionService feignYcqhActionService = Feign.builder().client(new ApacheHttpClient())
                .encoder(new JacksonEncoder())
                .decoder(new JacksonDecoder())
                .options(new Request.Options(1000, 3500))
                .retryer(new Retryer.Default(5000, 5000, 3))
                .target(FeignYcqhActionService.class, baseUrl);

        if (null != feignYcqhActionService) {
            log.info("feignYcqhActionService 创建成功!");
        } else {
            throw new RuntimeException("feignYcqhActionService 创建失败");
        }

        return feignYcqhActionService;
    }
}

3、编写Feign调用服务

import com.foresee.sznf.wxgzh.model.ycqh.*;
import feign.Param;
import feign.RequestLine;

import java.util.List;

/**
 * @author chenweifeng
 * @date 2019/11/13 10:59
 * 
 **/
public interface FeignYcqhActionService {

    /**
     * 获取列表
     *
     * @param id
     * @return
     */
    @RequestLine("GET /xxxxxxx?id={id}")
    YcqhActionReponse getList(@Param(value = "id") String id);
}

其中YcqhActionReponse是根据第三方接口返回的json去编写的,你也可以直接用JsonObject接收。

4、编写controller测试

import com.foresee.sznf.wxgzh.model.ycqh.*;
import com.foresee.sznf.wxgzh.service.FeignYcqhActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author chenweifeng
 * @date 2019/11/13 15:04
 **/
@Controller
@RequestMapping("/test/ycqh")
public class FeginYcqhController {

    @Autowired
    private FeignYcqhActionService ycqhActionService;

    /**
     * 获取大厅
     *
     * @param organid
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public YcqhActionReponse getList(String id) {
        YcqhActionReponse ycqhActionReponse = ycqhActionService.getList(id);
        return ycqhActionReponse;
    }
}

好的,这样就可以实现http请求第三方接口了!