Like Autwired in ConversionService in springboot

Trying to access the ConversionControl model in Springboot was out of luck.

@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; } } 

The following error is displayed:

 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)} 
+4
source share
2 answers

There seems to be no ConversionService bean available, judging by the last nested exception:

 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. 

Take a look at the Spring documentation for you to declare a ConversionService bean. In the XML configuration, it will look like this:

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

And since you are using Spring Boot, I assume that you are creating the context programmatically, so you should create a method annotated with @Bean that returns a ConverstionService like this ( here ):

 @Bean(name="conversionService") public ConversionService getConversionService() { ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean(); bean.setConverters(...); //add converters bean.afterPropertiesSet(); return bean.getObject(); } 
+4
source

I do not completely agree with the accepted answers, since by default there will be a ConverstionService named mvcConversionService , so you will get a duplicate bean exception . Instead of addConverter to FormatterRegistry , here is a link to answer the part:

Java Config equivalent for conversion Service / Formatting ConversionServiceFactoryBean

You will also need (in some cases) to define at least an empty Component for the ConversionService , as shown below:

 @Component @Primary public class MyConversionService extends DefaultConversionService implements ConversionService { // an empty ConversionService to initiate call to register converters } 

This means that the spring container initiates a call:

 class WebMvcConfigurerAdapter { ... public void addFormatters(FormatterRegistry registry) { //registry.addConverter(...); } } 
+1
source

Source: https://habr.com/ru/post/981452/


All Articles