• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解spring cloud feign踩坑记录

java 搞代码 4年前 (2022-01-05) 23次浏览 已收录 0个评论

这篇文章主要介绍了spring cloud feign踩坑记录,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

1:多客户端时,feign接口抽取到公共jar中,此时,客户端的启动类上需要对该jar中feign所在的包进行扫描,要在spring和feign中同时注册,否则启动时会报:“Consider defining a bean of type ‘******Feign’ in your configuration.”

 @SpringBootApplication @EnableTransactionManagement @EnableDiscoveryClient @ComponentScan(basePackages={"com.lcamtech.aidis.feign","com.lcamtech.aiads.dts"}) @EnableFeignClients(basePackages = {"com.lcamtech.aidis.feign"}) @EnableCaching @MapperScan(basePackages = "com.lcamtech.aiads.dts.mapper") public class Application extends SpringBootServletInitializer{ public static void main(String[] args) { SpringApplication.run(Application.class, args); } } 

重点:

 @ComponentScan(basePackages={"com.lcamtech.aidis.feign","com.lcamtech.aiads.dts"}) @EnableFeignClients(basePackages = {"com.lcamtech.aidis.feign"})

aidis包为包含feign的jar, 此时@ComponentScan还需要同时扫描本项目的包。

2:使用Fegin传值时,GET变POST

 @FeignClient(value = "SERVICE-NAME") public interface UserAccountFeign { @RequestMapping(value = "/ac/exist", method = RequestMethod.GET) public BaseResult isExist(@RequestParam("mobile") String mobile); } 

feign在传递时默认会将数据放在RequestBody中,所以会导致默认使用POST请求(及时@RequestMapping写着GET也没用),此时需要在参数列表中声明@RequestParam才能进行正常的GET请求。

3:feign请求返回复杂对象时

如:

 public class Result{ private string code; private string message; private Object data; //get/set } 

问题描述:当请求返回的是Result的一个对象时,

来源gaodai.ma#com搞##代!^码网

对于该对象内部的data值,会变成一个linkedHashMap,并不会被转换成相应的类对象,若直接强转会报类型错误。

解决方法1:简单转换

 /** * @Description: 将数据转换到相应的容器 * @param bean * @param clazz * @return * @throws * @author SunF * @date 2018/6/20 10:28 */ public static  T convertValue(Object bean, Class clazz){ try{ ObjectMapper mapper = new ObjectMapper(); return mapper.convertValue(bean, clazz); }catch(Exception e){ log.error("错误的转换:BeanUtil.convertValue() --->" + e.getMessage()); return null; } } 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持gaodaima搞代码网

以上就是详解spring cloud feign踩坑记录的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解spring cloud feign踩坑记录

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址