springboot集成feign的自定義start
分享一個項目:
項目地址:https://github.com/cumt-cx/spring-boot-lettelesd-feign-start.git
feign集成至springBoot,自定義了部分常用的配置
項目介紹
自定義的spring-boot的feign starter,爲更方便的feign的使用並集成spring-boot的auto configuration
打包
修改相關的maven私服地址,在feign工程下mvn clean install
使用方式
依賴
<dependency>
<groupId>com.littlesd.start</groupId>
<artifactId>feign</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
集成
在spring-boot項目的application.yml文件中加入配置實例如下:feign:
clients:
github:
base-url: https://api.github.com
request-headers:
K-AppCode: java
K-AppKey: helloworld
K-SignVer: 2
huobi:
base-url: https://api.huobi.pro/market
loglovel: FULL
使用
* 將上述配置項賦予正確的值
* interface定義以及返回http序列化對象定義
package com.littlesd.demo.feign.github;
import javax.ws.rs.*;
@Path("/")
public interface GitHubClientAPI {
@GET
@Path("/users/{userName}")
GitHubUser getUser(@PathParam("userName") String userName);
}
* 接口包的掃描
@SpringBootApplication
@FeignClients({
@FeignClient(
name = "github", scanPackages = {
"com.littlesd.demo.feign.github"
})
})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
* GitHubClientAPI的使用
@RestController
@RequestMapping("/github")
public class GitHubWeb {
@Autowired
private GitHubClientAPI gitHubClientAPI;
@RequestMapping(value = "/users/{user}")
public GitHubUser getUser(@PathVariable(value = "user")String userName){
return gitHubClientAPI.getUser(userName);
}
}