Spring feign時設置header信息
最近使用 SpringBoot 項目,把一些 http 請求轉為 使用 feign方式。但是遇到一個問題:個別請求是要設置header的。
于是,查看官方文檔和博客,大致推薦兩種方式。也可能是我沒看明白官方文檔。
接口如下:
1
2
3
4
5
6
7
|
@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注解。直接在請求上或者在類上添加
這種方式經過嘗試,沒有作用。暫時不清楚原因。
2. 通過實現RequestInterceptor接口,完成對所有的Feign請求,設置Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@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; } } |
這種方式,是針對所有feign請求進行攔截,設置Header,不適于我的需求。
后來發現其實我的思路走偏了。咨詢了一個同事,既然使用的是RequestMapping注解。那么直接使用RequestMapping注解的header屬性就可以了。如下:
1
|
@RequestMapping (value = "/xx" , method = RequestMethod.POST, headers = { "content-type=application/x-www-form-urlencoded" }) |
有一點需要注意:content-type=application/x-www-form-urlencoded。此時,方法里接收的參數,就不能直接是一個對象(Map等)。不然還是會默認
content-type為 application/json.
1
2
|
@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動態設置Header
Feign調用接口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
/** * @author Liangzhifeng * date: 2018/9/13 */ public interface UserInfoFeignClient { /** * 根據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 { /** * 授權信息Header的key */ public static final String OAUTH_KEY = "token" ; /** * 授權信息Header的值的前綴 */ public static final String OAUTH_VALUE_PREFIX = "Bearer " ; // GlobalConstant.AUTHORITY_SERVICE_LINK : 服務的名稱 @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; } } |
接口調用:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@Autowired private AuthorityConfig authorityConfig; /** * 根據token獲取用戶信息 * * @param token 用戶登錄的授權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 ; } } |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://www.cnblogs.com/hui-run/p/8969702.html