Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Register Spring Converter Programmatically in Spring Boot

Tags:

I want to register a Spring Converter in a Spring Boot project programmatically. In past Spring projects I've done it in XML like this...

<!-- Custom converters to allow automatic binding from Http requests parameters to objects --> <!-- All converters are annotated w/@Component --> <bean id="conversionService"       class="org.springframework.context.support.ConversionServiceFactoryBean">     <property name="converters">         <list>             <ref bean="stringToAssessmentConverter" />         </list>     </property> </bean> 

I'm trying to figure out how to do in Spring Boot's SpringBootServletInitializer

Update: I've made a little progress by passing the StringToAssessmentConverter as an argument to getConversionService, but now I'm getting a "No default constructor found" error for the StringToAssessmentConverter class. I'm not sure why Spring is not seeing the @Autowired constructor.

@SpringBootApplication public class Application extends SpringBootServletInitializer {      ...      @Bean(name="conversionService")     public ConversionServiceFactoryBean getConversionService(StringToAssessmentConverter stringToAssessmentConverter) {         ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();          Set<Converter> converters = new HashSet<>();          converters.add(stringToAssessmentConverter);          bean.setConverters(converters);         return bean;     } }   

Here's the code for the Converter...

 @Component  public class StringToAssessmentConverter implements Converter<String, Assessment> {       private AssessmentService assessmentService;       @Autowired      public StringToAssessmentConverter(AssessmentService assessmentService) {          this.assessmentService = assessmentService;      }       public Assessment convert(String source) {          Long id = Long.valueOf(source);          try {              return assessmentService.find(id);          } catch (SecurityException ex) {              return null;          }      }  } 

Full Error

Failed to execute goal org.springframework.boot:spring-boot-maven- plugin:1.3.2.RELEASE:run (default-cli) on project yrdstick: An exception  occurred while running. null: InvocationTargetException: Error creating  bean with name 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPo stProcessor': Invocation of init method failed; nested exception is  org.springframework.beans.factory.UnsatisfiedDependencyException: Error  creating bean with name 'conversionService' defined in  me.jpolete.yrdstick.Application: Unsatisfied dependency expressed through  constructor argument with index 0 of type  [me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: : Error  creating bean with name 'stringToAssessmentConverter' defined in file  [/yrdstick/target/classes/me/jpolete/yrdstick/websupport /StringToAssessmentConverter.class]: Instantiation of bean failed; nested  exception is org.springframework.beans.BeanInstantiationException: Failed  to instantiate  [me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: No default  constructor found; nested exception is java.lang.NoSuchMethodException:  me.jpolete.yrdstick.websupport.StringToAssessmentConverter.<init>();  nested exception is  org.springframework.beans.factory.BeanCreationException: Error creating  bean with name 'stringToAssessmentConverter' defined in file [/yrdstick /dev/yrdstick/target/classes/me/jpolete/yrdstick/websupport /StringToAssessmentConverter.class]: Instantiation of bean failed; nested  exception is org.springframework.beans.BeanInstantiationException: Failed  to instantiate  [me.jpolete.yrdstick.websupport.StringToAssessmentConverter]: No default  constructor found; nested exception is java.lang.NoSuchMethodException:  me.jpolete.yrdstick.websupport.StringToAssessmentConverter.<init>() 
like image 975
jpolete Avatar asked Jan 26 '16 22:01

jpolete


People also ask

How does a spring converter work?

Spring provides out-of-the-box various converters for built-in types; this means converting to/from basic types like String, Integer, Boolean and a number of other types. Apart from this, Spring also provides a solid type conversion SPI for developing our custom converters.


1 Answers

The answer is, you only need to anotate your converter as @Component:

This is my converter example

import org.springframework.core.convert.converter.Converter; @Component public class DateUtilToDateSQLConverter implements Converter<java.util.Date, Date> {      @Override     public Date convert(java.util.Date source) {         return new Date(source.getTime());     } } 

Then when Spring needs to make convert, the converter is called.

My Spring Boot Version: 1.4.1

like image 97
deFreitas Avatar answered Oct 14 '22 00:10

deFreitas