0

I have read few posts, and tried implementing the same way, but somehow i am getting below error. I want to implement through XML based configuration.

Two Ways

Annotation Based - Works Fine

@ControllerAdvice
@Controller
public class AppBindingInitializer {

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
    }

}

XML Based - Getting Error

<bean id="coreCustomEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
     <property name="customEditors">
        <map>
            <entry key="java.lang.String">
                <bean class="org.springframework.beans.propertyeditors.StringTrimmerEditor">
                    <constructor-arg name="emptyAsNull" value="true" />
                </bean>
            </entry>
        </map>
    </property>
 </bean>

Exception

Caused by: org.springframework.beans.TypeMismatchException: Failed to convert property value of type 'java.util.LinkedHashMap' to required type 'java.util.Map' for property 'customEditors'; nested exc
eption is java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.beans.propertyeditors.StringTrimmerEditor] to required type [java.lang.Class] for property 'customEditor
s[java.lang.String]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [org.springframework.beans.propertyeditors.StringTrimmerEditor]
        at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:479)
        at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:511)
        at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:505)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1502)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1461)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
        ... 53 more
Caused by: java.lang.IllegalArgumentException: Cannot convert value of type [org.springframework.beans.propertyeditors.StringTrimmerEditor] to required type [java.lang.Class] for property 'customEdito
rs[java.lang.String]': PropertyEditor [org.springframework.beans.propertyeditors.ClassEditor] returned inappropriate value of type [org.springframework.beans.propertyeditors.StringTrimmerEditor]
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:263)
        at org.springframework.beans.TypeConverterDelegate.convertToTypedMap(TypeConverterDelegate.java:623)
        at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:208)
        at org.springframework.beans.BeanWrapperImpl.convertIfNecessary(BeanWrapperImpl.java:459)
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116

1 Answers1

0

You have to specify a class name as value, not a bean (as stated by the message), like this:

<bean id="coreCustomEditors" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
     <property name="customEditors">
         <map>
             <entry key="java.lang.String" value="your.package.EmptyAsNullStringTrimmerEditor"/>
         </map>
    </property>
</bean>

with EmptyAsNullStringTrimmerEditor being:

package your.package;

public class EmptyAsNullStringTrimmerEditor extends StringTrimmerEditor {
    public YourClass() { super(true); }
}

see CustomEditorConfigurer javadoc

  • how to pass `` – Ankur Singhal Dec 09 '14 at 07:16
  • In 2nd option how does it identify which editor for which type of datatype, unlike in first option we provide `key as java.lanag.String' and values as its editor – Ankur Singhal Dec 09 '14 at 07:28
  • Sorry I misread the doc, you have to subclass here, see my edit –  Dec 09 '14 at 08:18
  • i tried with you approach, made all possible changes, adding depends on etc, but seems like doing through xml, it is not even registering my beans. but if i follow my first approach seems tpo work fine. – Ankur Singhal Dec 09 '14 at 09:40