feign为我们带来简洁风格rest客户端的同时也带来了一些麻烦,因为稍微对他不了解就可能走弯路。
依赖
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-core</artifactId>
<version>8.18.0</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-gson</artifactId>
<version>8.18.0</version>
</dependency>
<dependency>
<groupId>com.netflix.feign</groupId>
<artifactId>feign-slf4j</artifactId>
<version>8.4.1</version>
</dependency>
demo展示了重写url和自定义请求头
public interface XxxRestClient {
@RequestLine("POST /api/xxx/list")
@Headers("Content-Type: application/json")
XxxResp uploadFile(@RequestBody XxxReq req);
}
@Configuration
@Slf4j
public class XxxRestConf {
@Bean
public XxxRestClient newXxxRestClient () {
return Feign.builder().requestInterceptor(new RequestInterceptor() {
@Override
public void apply(RequestTemplate template) {
String json = new String(template.body());
if (StringUtils.isNotEmpty(json)) {
JSONObject jsonObject = JSONObject.parseObject(json);
String secret = jsonObject.getString("RSAstring");
if (template.url().equals(url)) {
try {
// 重写请求uri,增加加密串
template.insert(url.length(), "?RSAString=" + URLEncoder.encode(secret, Charsets.UTF_8.toString()));
} catch (UnsupportedEncodingException e) {
log.error("", e);
}
}
// 在请求发送之前重写头信息
template.header("token", token);
}
}
}
})
.encoder(new GsonEncoder())
.decoder(new GsonDecoder())
//关闭重试功能
.retryer(Retryer.NEVER_RETRY)
//打印完整的http请求响应日志
.logLevel(Logger.Level.FULL)
.logger(new Slf4jLogger())
.target(XxxRestClient.class, "http://127.0.0.1:8080");
}
}
注意声明feign日志级别
如logback.xml新增
<logger name="feign" level="DEBUG"/>
效果
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] ---> POST http://127.0.0.1/api/list HTTP/1.1
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] Content-Type: application/json
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] Content-Length: 177
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus]
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] {
"id": 2941587,
"systemId": 21,
"signature": "5c168dd2891f85dfd764196373fc1d1b",
"signtype": "md5",
}
2019-09-27 10:44:08.891 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] ---> END HTTP (177-byte body)
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] <--- HTTP/1.1 200 OK (170ms)
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] cache-control: no-cache
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] content-length: 115
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] content-type: application/json; charset=utf-8
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] date: Fri, 27 Sep 2019 02:43:46 GMT
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] expires: -1
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] pragma: no-cache
2019-09-27 10:44:09.062 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] server: Microsoft-IIS/7.5
2019-09-27 10:44:09.063 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] x-aspnet-version: 4.0.30319
2019-09-27 10:44:09.063 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] x-powered-by: ASP.NET
2019-09-27 10:44:09.063 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus]
2019-09-27 10:44:09.063 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] {"result":1,"message":"查询成功","data":{"state":1,"date":"2019-09-25 17:39:04","cnt":1.00}}
2019-09-27 10:44:09.063 [http-apr-8080-exec-2] DEBUG
feign.Logger - [PowerRestClient#BuyPowerStatus] <--- END HTTP (115-byte body)