通过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请求第三方接口了!
上一篇: 数据库系统概念 读书笔记(五)
推荐阅读
-
android中可以通过两种方式调用接口发送短信
-
使用Spring Cloud Feign作为HTTP客户端调用远程HTTP服务的方法(推荐)
-
Python调用C++,通过Pybind11制作Python接口
-
python itchat实现调用微信接口的第三方模块方法
-
在jquery中的ajax方法怎样通过JSONP进行远程调用
-
Python 通过调用接口获取公交信息的实例
-
(springCloud-8 Zuul网关路由的基本配置feign实现接口调用)
-
HttpClient 远程接口调用方式
-
ComboBox(下拉列表框)通过url加载调用远程数据的方法
-
jquery中的ajax方法怎样通过JSONP进行远程调用