Строка пустая к нулю - пустые поля формы к нулю - Spring Editor - StringTrimmerEditor

Я прочитал несколько постов и попытался реализовать таким же образом, но почему-то я получаю ниже ошибки. Я хочу реализовать через XML based configuration,

Два пути

Аннотация на основе - отлично работает

@ControllerAdvice
@Controller
public class AppBindingInitializer {

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

}

На основе XML - получение ошибки

<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>

исключение

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)

1 ответ

Вы должны указать имя класса в качестве значения, а не bean-компонент (как указано в сообщении), например:

<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>

с EmptyAsNullStringTrimmerEditor являются:

package your.package;

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

увидеть CustomEditorConfigurer Javadoc

Другие вопросы по тегам