13

Trying to access the ConversionControl in model in springboot, no 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;
    }
}

It shows the following error:

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)}
Michael Petch
  • 43,801
  • 8
  • 98
  • 174
lumi
  • 143
  • 1
  • 1
  • 7

2 Answers2

8

Apparently there is 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.

A look into the Spring documentation reveals, that you should declare a ConversionService bean. In the XML configuration it would 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're using Spring Boot, I assume you are creating the context programatically, so you should create a method annotated with @Bean, which returns a ConverstionService, like this (explained here):

@Bean(name="conversionService")
public ConversionService getConversionService() {
    ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
    bean.setConverters(...); //add converters
    bean.afterPropertiesSet();
    return bean.getObject();
}
Community
  • 1
  • 1
lmazgon
  • 1,117
  • 1
  • 20
  • 38
  • thanks for your comments..but what is bean.setConverters(...); it shows an error - org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type com.lumiin.mytalk.dto.CityDTO to type com.lumiin.mytalk.model.City – lumi May 07 '15 at 20:45
  • You probably didn't specify a converter - you have to create a class, which implements the interface `Converter`, and another one the other way around, then register both in the `ConversionServiceFactoryBean`. Please look at this tutorial: http://www.javabeat.net/introduction-to-spring-converters-and-formatters/ – lmazgon May 07 '15 at 21:01
  • In the Javaconfig, You don't even need to return a `ConverstionService` from the `ConversionServiceFactoryBean` like the example above, just return a `ConversionServiceFactoryBean` bean and spring will do the rest for you – cdxf Apr 12 '18 at 01:46
4

Not totally agreed with the accepted answers, because there would be a default ConverstionService named mvcConversionService so you would get duplicate bean exception. Instead addConverter to FormatterRegistry, here is the link for the part answer:

Java Config equivalent for conversionService / FormattingConversionServiceFactoryBean

Also you would need (in some cases) to define to at least an empty Component for ConversionService, something like below:

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

This is to force spring container to initiate a call to:

class WebMvcConfigurerAdapter {
    ...

    public void addFormatters(FormatterRegistry registry) {
         //registry.addConverter(...);
    }
}
vijay
  • 729
  • 7
  • 13
  • 3
    the `mvcConversionService` will only be present if you have `org.springframework.boot:spring-boot-starter-web` dependency in your project – anton1980 Sep 24 '18 at 14:07