Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在springboot的ConversionService中自动连接_Java_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

Java 如何在springboot的ConversionService中自动连接

Java 如何在springboot的ConversionService中自动连接,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,试图访问springboot中模型中的ConversionControl,运气不佳 @Component public class CityHelperService { @Autowired ConversionService conversionService;// = ConversionServiceFactory.registerConverters(); public City toEntity(CityDTO dto){ City en

试图访问springboot中模型中的ConversionControl,运气不佳

@Component
public class CityHelperService  {

    @Autowired
    ConversionService conversionService;// = ConversionServiceFactory.registerConverters();

    public City toEntity(CityDTO dto){
        City entity = conversionService.convert(dto, City.class);
        return entity;
    }

    public CityDTO toDTO(City entity){
        CityDTO dto = conversionService.convert(entity, CityDTO.class);
        return dto;
    }
}
它显示以下错误:

Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.lumiin.mytalk.model.CityModel com.lumiin.mytalk.controllers.CityController.cityModel;
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cityModel' defined in file : Unsatisfied dependency expressed through constructor argument with index 1 of type [com.lumiin.mytalk.dao.CityHelperService]: : Error creating bean with name 'cityHelperService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.core.convert.ConversionService com.lumiin.mytalk.dao.CityHelperService.conversionService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)};
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cityHelperService': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.core.convert.ConversionService com.lumiin.mytalk.dao.CityHelperService.conversionService;
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

从最后一个嵌套异常判断,显然没有可用的
ConversionService
bean:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.core.convert.ConversionService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
查看后发现,您应该声明一个
ConversionService
bean。在XML配置中,它将如下所示:

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="example.MyCustomConverter"/>
        </set>
    </property>
</bean>

不完全同意接受的答案,因为将有一个名为
mvcConversionService
的默认
ConversationService
,因此您将得到重复bean异常。相反,将Converter添加到
FormatterRegistry
,以下是部分答案的链接:

您还需要(在某些情况下)为
转换服务
定义至少一个空的
组件
,如下所示:

@Component @Primary
public class MyConversionService extends DefaultConversionService implements ConversionService {
    // an empty ConversionService to initiate call to register converters
}
这将强制spring容器启动对以下对象的调用:

class WebMvcConfigurerAdapter {
    ...

    public void addFormatters(FormatterRegistry registry) {
         //registry.addConverter(...);
    }
}

谢谢你的评论。但是什么是bean.setConverters(…);它显示了一个错误-org.springframework.core.convert.ConverterNotFoundException:找不到能够从com.lumiin.mytalk.dto.CityDTO类型转换为com.lumiin.mytalk.model.CityYou可能没有指定转换器-您必须创建一个类,该类实现接口
转换器
,另一个反过来,然后在
转换服务工厂bean
中注册这两个。请看本教程:在Javaconfig中,您甚至不需要像上面的示例那样从
ConversionServiceFactoryBean
返回
ConversationService
,只需返回一个
ConversionServiceFactoryBean
bean,spring将为您完成其余的工作
mvcConversionService
仅当您的项目中有
org.springframework.boot:spring boot starter web
依赖项时才会出现
class WebMvcConfigurerAdapter {
    ...

    public void addFormatters(FormatterRegistry registry) {
         //registry.addConverter(...);
    }
}