Monday, June 4, 2012

Spring Custom Converter

Recently I ran into a problem where Spring would automatically convert comma separated String into String array, and quick search in google, I found people have the same problem on stackoverflow.com
http://stackoverflow.com/questions/4998748/how-to-prevent-parameter-binding-from-interpreting-commas-in-spring-3-0-5

Read more about Spring Converter http://static.springsource.org/spring/docs/current/spring-framework-reference/htmlsingle/spring-framework-reference.html#core-convert

Just a summary of the solution.

1. Create Custom Converter for String to String[]

import org.springframework.core.convert.converter.Converter;
import org.springframework.util.StringUtils;

public class CustomStringToArrayConverter implements Converter{
   @Override
    public String[] convert(String source) {
        return StringUtils.delimitedListToStringArray(source, ";");
    }
}

2. Register the Custom Conveter.

specify this conversion service bean in your configuration:


    
        
            
        
    

1 comment: