简体   繁体   中英

Spring MVC data binding

I have a very strange error which I managed to fix, but I can't understand why it appeared at first place.

I had a Spring MVC application with many different classes that was scaffolded by the Spring Roo, like: AuthenticationConfig, ConnectorConfig etc. After scaffolding I pushed all code from AJ to java and all worked fine, and I modified it to suit my needs.

But one day I decided to refactor some of these classes (because they had a lot in common), and bindings broke up.

I started to receive binding errors:

Failed to convert property value of type 'java.lang.String' to required type 'com.mypackage.GeneralConfig'.

After I registered String to GeneralConfig converter in FormattingConversionServiceFactoryBean error was gone (it already had GeneralConfig to String converter), but I do not understand why everything worked fine before. Everything I did was removed unnecessary configuration classes and replaced them with one general class, like this:

@ManyToOne
private ConnectorConfig connector;

@ManyToOne
private XUIDMapperConfig xuidMapper;

@ManyToOne
private AuthenticationTokenConfig authenticationToken;

To

@ManyToOne
private GeneralConfig connector;

@ManyToOne
private GeneralConfig xuidMapper;

@ManyToOne
private GeneralConfig authenticationToken;

Maybe I missed something important during refactoring?

Check ApplicationConversionServiceFacotryBean.

Roo should have created that class in the "web" or "controller" package, along with ApplicationConversionServiceFactoryBean_Roo_ConversionService.aj that you should have inlined. Perhaps you missed that class while inlining?

For others facing that issue, make sure you have the following:

public Converter<String, GeneralConfig> getStringToGeneralConfigConverter() {
    return new org.springframework.core.convert.converter.Converter<java.lang.String, GeneralConfig>() {
        public GeneralConfig convert(String id) {
            return getObject().convert(getObject().convert(id, Long.class), GeneralConfig.class);
        }
    };
}

and that converter installed:

public void installLabelConverters(FormatterRegistry registry) {
    ...
    registry.addConverter(getStringToConfigurationConverter());
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM