LianjiaTech / retrofit-spring-boot-starter

A spring-boot starter for retrofit, supports rapid integration and feature enhancements.(适用于retrofit的spring-boot-starter,支持快速集成和功能增强)
Apache License 2.0
1.78k stars 340 forks source link

无法获取全局转换器工厂Bean #144

Closed hunao0221 closed 1 year ago

hunao0221 commented 1 year ago

在配置文件中配置了转换工厂为fastjson的Retrofit2ConverterFactory

retrofit:
  # 全局转换器工厂
  global-converter-factories:
    - com.github.lianjiatech.retrofit.spring.boot.core.BasicTypeConverterFactory
    - com.alibaba.fastjson.support.retrofit.Retrofit2ConverterFactory

并且注册了Bean

    @Bean
    public Converter.Factory converterFactory() {
        return Retrofit2ConverterFactory.create();
    }

应用启动时警告日志,提示找不到Retrofit2ConverterFactory Bean

2022-12-13 09:44:51.306 [main] WARN  c.g.l.r.spring.boot.util.AppContextUtils(AppContextUtils.java:26) - Failed to get bean from applicationContext!
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.alibaba.fastjson.support.retrofit.Retrofit2ConverterFactory' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1172)
    at com.github.lianjiatech.retrofit.spring.boot.util.AppContextUtils.getBeanOrNew(AppContextUtils.java:23)
    at com.github.lianjiatech.retrofit.spring.boot.core.RetrofitFactoryBean.lambda$createRetrofit$3(RetrofitFactoryBean.java:216)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
    at com.github.lianjiatech.retrofit.spring.boot.core.RetrofitFactoryBean.createRetrofit(RetrofitFactoryBean.java:215)
    at com.github.lianjiatech.retrofit.spring.boot.core.RetrofitFactoryBean.getObject(RetrofitFactoryBean.java:67)

如果找不到Bean的话,retrofit-spring-boot-starter是会用反射的方式获取的,因此这里并不影响功能。 不过不确定这是不是一个bug,请确认。

hunao0221 commented 1 year ago

看源码找到解决方法,应该返回具体的实现,而不是接口

    @Bean
    public Retrofit2ConverterFactory converterFactory() {
        return Retrofit2ConverterFactory.create();
    }