Spring 使用 feign時(shí)設(shè)置header信息的操作
Spring feign時(shí)設(shè)置header信息
最近使用 SpringBoot 項(xiàng)目,把一些 http 請(qǐng)求轉(zhuǎn)為 使用 feign方式。但是遇到一個(gè)問題:個(gè)別請(qǐng)求是要設(shè)置header的。
于是,查看官方文檔和博客,大致推薦兩種方式。也可能是我沒看明白官方文檔。
接口如下:
@FeignClient(url ="XX_url", value ="XXService")
public interface XXService {
@RequestMapping(value ="/xx", method = RequestMethod.POST)
@Headers({"Content-Type: application/json","Accept: application/json"})
String sendDing(String params);
}
1. 使用Headers注解。直接在請(qǐng)求上或者在類上添加
這種方式經(jīng)過嘗試,沒有作用。暫時(shí)不清楚原因。
2. 通過實(shí)現(xiàn)RequestInterceptor接口,完成對(duì)所有的Feign請(qǐng)求,設(shè)置Header
@Component
public class FeginClientConfig {
@Bean
public RequestInterceptor headerInterceptor() {
return new RequestInterceptor() {
@Override
public void apply(RequestTemplate requestTemplate) {
// 小示例,沒什么卵用
requestTemplate.header("Content-Type","application/json");
}
};
}
@Bean
public Logger.Level level() {
return Logger.Level.FULL;
}
}
這種方式,是針對(duì)所有feign請(qǐng)求進(jìn)行攔截,設(shè)置Header,不適于我的需求。
后來(lái)發(fā)現(xiàn)其實(shí)我的思路走偏了。咨詢了一個(gè)同事,既然使用的是RequestMapping注解。那么直接使用RequestMapping注解的header屬性就可以了。如下:
@RequestMapping(value ="/xx", method = RequestMethod.POST, headers = {"content-type=application/x-www-form-urlencoded"})
有一點(diǎn)需要注意:content-type=application/x-www-form-urlencoded。此時(shí),方法里接收的參數(shù),就不能直接是一個(gè)對(duì)象(Map等)。不然還是會(huì)默認(rèn)
content-type為 application/json.
@RequestMapping(value ="/xx", method = RequestMethod.POST, headers = {"content-type=application/x-www-form-urlencoded"})
String login(@RequestParam("username") String username,@RequestParam("password") String password;
Feign動(dòng)態(tài)設(shè)置Header
Feign調(diào)用接口:
/**
* @author Liangzhifeng
* date: 2018/9/13
*/
public interface UserInfoFeignClient {
/**
* 根據(jù)token獲取用戶信息
* @param token
* @return
*/
@RequestMapping(value = "/user/info/1.0", method = RequestMethod.POST)
Object getUserInfoByToken(@RequestParam("token") String token);
}
/**
* @author Liangzhifeng
* date: 2018/9/15
*/
@Component
public class AuthorityConfig {
/**
* 授權(quán)信息Header的key
*/
public static final String OAUTH_KEY = "token";
/**
* 授權(quán)信息Header的值的前綴
*/
public static final String OAUTH_VALUE_PREFIX = "Bearer ";
// GlobalConstant.AUTHORITY_SERVICE_LINK : 服務(wù)的名稱
@Autowired
private Client client;
public UserInfoFeignClient userInfoFeignClient(String token) {
UserInfoFeignClient authorityServiceLoginInvoker = Feign.builder().client(client)
.encoder(new GsonEncoder())
.decoder(new GsonDecoder())
.contract(new SpringMvcContract())
.requestInterceptor(template -> template.header(OAUTH_KEY, OAUTH_VALUE_PREFIX + token))
.target(UserInfoFeignClient.class, GlobalConstant.AUTHORITY_SERVICE_LINK);
return authorityServiceLoginInvoker;
}
}
接口調(diào)用:
@Autowired
private AuthorityConfig authorityConfig;
/**
* 根據(jù)token獲取用戶信息
*
* @param token 用戶登錄的授權(quán)token
* @return 用戶信息JSON
*/
public Object getUserInfo(String token) {
try {
Object userInfo = authorityConfig.userInfoFeignClient(token).getUserInfoByToken(token);
return userInfo;
} catch (Exception e) {
log.info("獲取用戶信息異常", e);
return null;
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
如何優(yōu)雅的拋出Spring Boot注解的異常詳解
這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅的拋出Spring Boot注解的異常的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
實(shí)戰(zhàn)指南:Java編寫Flink?SQL解決難題
想知道如何利用Java編寫Flink?SQL解決難題嗎?本指南將為您揭示最實(shí)用的技巧和策略,讓您輕松應(yīng)對(duì)挑戰(zhàn),跟著我們一起探索,讓Java和Flink?SQL成為您問題解決的得力助手!2023-12-12
Java修飾符 abstract,static,final 的區(qū)別詳解
以下是對(duì)Java修飾符abstract,static,final的區(qū)別進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來(lái)參考下2013-09-09
idea中創(chuàng)建多module的maven工程的方法
這篇文章主要介紹了idea中創(chuàng)建多module的maven工程的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-10-10
Spring中一個(gè)少見的引介增強(qiáng)IntroductionAdvisor
這篇文章主要為大家介紹了Spring中一個(gè)少見的引介增強(qiáng)IntroductionAdvisor實(shí)戰(zhàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

