📚 开源资源分享——颜佐光的个人书架
✉️ 联系站长
书架 / Java / SpringCloud

SpringCloud使用Feign对接第三方http接口

2020-08-08 20:23 · 约 852 字 · 浏览 5,763 次SpringCloud

SpringCloud使用Feign对接第三方http接口

1.依赖

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>

2.写客户端去请求

/**
* 查询楼层列表
* @return
*/
@RequestMapping(value = "space/list", method = RequestMethod.POST, consumes=MediaType.APPLICATION_JSON_VALUE)
Result<List<SFlatInfoDto>> getFlatList(
    @RequestBody SFlatInfoDto dto
);

  • method 什么类型,POST还是GET
  • MediaType 参数以什么方式去请求
  • SFlatInfoDto 传递的参数对象

3.在客户端上加注解

@FeignClient(url = "${fegin-inter-api.flatUrl}", name = "flatUrl")
  • fegin-inter-api.flatUrl 在配置文件里面写着,例如http:demo/ 那么你请求的,就是http:demo/space/list
  • name 全局唯一,不能在别的client里面重复,不然会报错

4.action里面调用

先引入client

@Autowired
private SFlatInfoFeign sFlatInfoFeign;
//直接调用即可
sFlatInfoFeign.getFlatList(sFlatInfoDto);
← 返回《Java》目录书架 →