1
votes

I have this converter to trim whitespaces

@Component
public class StringTrimmingConverter implements Converter<String, String> {
    @Override
    public String convert(String source) {
        return source.trim();
    }
 }

But I'm getting this error upon startup of the web app project...

Caused by: java.lang.IllegalArgumentException: Unable to the determine sourceType and targetType which your Converter converts between; declare these generic types. at org.springframework.core.convert.support.GenericConversionService.addConverter(GenericConversionService.java:95) at org.springframework.core.convert.support.ConversionServiceFactory.registerConverters(ConversionServiceFactory.java:50) at org.springframework.context.support.ConversionServiceFactoryBean.afterPropertiesSet(ConversionServiceFactoryBean.java:69) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452) ... 21 more

Here's the configuration

<mvc:annotation-driven validator="validator" conversion-service="conversionService"/>

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean">
    <property name="converters">
        <set>
            <bean class="ph.com.xxx.yyy.converter.StringTrimmingConverter"/>
        </set>
    </property>
</bean>
2
Could you post your spring configuration?Kevin Bowersox
I updated the question and included the configuration.xxx
Try removing @ComponentKevin Bowersox
Try using a List instead of a Set in the configuration?Kevin Bowersox
I also tried that one before. It's not working. Maybe I'm missing something?xxx

2 Answers

0
votes

The bean is declared in your configuration and using annotations. I think the Converter bean should only be declared once:

//@Component Remove this
public class StringTrimmingConverter implements Converter<String, String> {
    @Override
    public String convert(String source) {
        return source.trim();
    }
 }
0
votes

I was able to fix this already by doing this instead of bean configuration in the applicationContext.

@Component("conversionService")
public class StringConverterService extends FormattingConversionService {
    public StringConverterService() {
        this.addConverter(new StringTrimmingConverter());
    }
}