feign踩坑_spring cloud fegin踩坑记录
1:多客戶端時,fegin接口抽取到公共jar中,此時,客戶端的啟動類上需要對該jar中fegin所在的包進行掃描,要在spring和fegin中同時注冊,否則啟動時會報:“Consider defining a bean of type '******Feign' in your configuration.”
@SpringBootApplication
@EnableTransactionManagement
@EnableDiscoveryClient
@ComponentScan(basePackages={"com.lcamtech.aidis.fegin","com.lcamtech.aiads.dts"})
@EnableFeignClients(basePackages = {"com.lcamtech.aidis.fegin"})
@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.fegin","com.lcamtech.aiads.dts"})
@EnableFeignClients(basePackages = {"com.lcamtech.aidis.fegin"})
aidis包為包含fegin的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);
}
fegin在傳遞時默認會將數(shù)據(jù)放在RequestBody中,所以會導致默認使用POST請求(及時@RequestMapping寫著GET也沒用),此時需要在參數(shù)列表中聲明@RequestParam才能進行正常的GET請求。
3:fegin請求返回復雜對象時
如:
public class Result{
private string code;
private string message;
private Object data;
//get/set
}
問題描述:當請求返回的是Result的一個對象時,對于該對象內(nèi)部的data值,會變成一個linkedHashMap,并不會被轉(zhuǎn)換成相應的類對象,若直接強轉(zhuǎn)會報類型錯誤。
解決方法1:簡單轉(zhuǎn)換
/**
* @Description: 將數(shù)據(jù)轉(zhuǎn)換到相應的容器
* @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("錯誤的轉(zhuǎn)換:BeanUtil.convertValue() --->" + e.getMessage());
return null;
}
}
2:
總結
以上是生活随笔為你收集整理的feign踩坑_spring cloud fegin踩坑记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在一个数组中找出和为目标值的那 两个 整
- 下一篇: linux系统管理实验报告总结_Linu