溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

spring cloud feign會遇到什么坑

發布時間:2021-08-26 13:15:11 來源:億速云 閱讀:145 作者:小新 欄目:編程語言

這篇文章將為大家詳細講解有關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的一個對象時,對于該對象內部的data值,會變成一個linkedHashMap,并不會被轉換成相應的類對象,若直接強轉會報類型錯誤。

解決方法1:簡單轉換

/**
   * @Description: 將數據轉換到相應的容器
   * @param bean
   * @param clazz
   * @return
   * @throws
   * @author SunF
   * @date 2018/6/20 10:28 
   */
  public static <T> T convertValue(Object bean, Class<T> clazz){
    try{
      ObjectMapper mapper = new ObjectMapper();
      return mapper.convertValue(bean, clazz);
    }catch(Exception e){
      log.error("錯誤的轉換:BeanUtil.convertValue() --->" + e.getMessage());
      return null;
    }
  }

關于“spring cloud feign會遇到什么坑”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女