这篇文章主要介绍“spring使用feign时怎么设置header信息”,在日常操作中,相信很多人在Spring使用feign时怎么设置header信息问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Sprin
这篇文章主要介绍“spring使用feign时怎么设置header信息”,在日常操作中,相信很多人在Spring使用feign时怎么设置header信息问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Spring使用feign时怎么设置header信息”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
最近使用 SpringBoot 项目,把一些 Http 请求转为 使用 feign方式。但是遇到一个问题:个别请求是要设置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);}
这种方式经过尝试,没有作用。暂时不清楚原因。
@Componentpublic 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属性就可以了。如下:
@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.
@RequestMapping(value ="/xx", method = RequestMethod.POST, headers = {"content-type=application/x-www-form-urlencoded"})String login(@RequestParam("username") String username,@RequestParam("passWord") String password;
public interface UserInfoFeignClient { @RequestMapping(value = "/user/info/1.0", method = RequestMethod.POST) Object getUserInfoByToken(@RequestParam("token") String token);}@Componentpublic class AuthorityConfig { public static final String OAUTH_KEY = "token"; 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; }}
@Autowiredprivate AuthorityConfig authorityConfig;public Object getUserInfo(String token) { try { Object userInfo = authorityConfig.userInfoFeignClient(token).getUserInfoByToken(token); return userInfo; } catch (Exception e) { log.info("获取用户信息异常", e); return null; }}
到此,关于“Spring使用feign时怎么设置header信息”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注编程网网站,小编会继续努力为大家带来更多实用的文章!
--结束END--
本文标题: Spring使用feign时怎么设置header信息
本文链接: https://www.lsjlt.com/news/299600.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
2024-05-09
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0