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; } }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。
新聞熱點
疑難解答
圖片精選