Spring type conversion make it possible to create converters which can convert between different types of objects. These converters can be registered to a conversion service. This service can be used to convert a given object to a given type.
To create a converter you have to write a class which implements the following interface.
public interface Converter<S, T> { T convert(S source); }
Built in converters
Several converter implementations are provided in the core.convert.support
package as a convenience. These include converters from Strings to Numbers and other common types. Consider StringToInteger
as an example Converter implementation:
final class StringToInteger implements Converter<String, Integer> { public Integer convert(String source) { return Integer.valueOf(source); } }
Creating custom converters
To create a custom converter you have to write a class similar to the above, where you describe how to convert from one type to another.
First let’s create two types. We will perform the conversion between them.
ConsumerEntity
contains the data of a consumer queried from the database.
public class ConsumerEntity { private int id; private String firstName; private String lastName; private String zipCode; private String city; private String street; // Getters and setters omitted for brevity }
ConsumerBo
on the other hand contains the fields that we would like to display about the consumer. It is called a BO (Business Object) because it contains data important to the business. In this case it means data we would like to display about a consumer.
public class ConsumerBo { private int id; private String name; private String address; // Getters and setters omitted for brevity }
Now, let’s create a class called ConsumerConverter
which will perform the conversion. Remember, this class must implement org.springframework.core.convert.support.Converter
.
import org.springframework.core.convert.converter.Converter; import conversion.example.ConsumerBo; import conversion.example.ConsumerEntity; public class ConsumerConverter implements Converter<ConsumerEntity, ConsumerBo>{ public ConsumerBo convert(ConsumerEntity source) { ConsumerBo result = new ConsumerBo(); result.setId(source.getId()); result.setName(source.getFirstName() + " " + source.getLastName()); result.setAddress(source.getZipCode() + ", " + source.getCity() + ", " + source.getStreet()); return result; } }
Register the custom converter
To use our custom converters, we have to register them in the application context xml using a bean definition like the following:
<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean"> <property name="converters"> <list> <bean class="conversion.example.converter.ConsumerConverter"/> </list> </property> </bean>
Invoking the conversion service
Below there is an examaple of using the conversion service we created. The service itself is autowired by Spring into the controller. This autowired object can be used to explicitly perform the conversion from one class to another.
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.convert.ConversionService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import conversion.example.ConsumerBo; import conversion.example.ConsumerEntity; @Controller public class HomeController { @Autowired ConversionService conversionService; @RequestMapping("home") public ModelAndView displayUser() { ModelAndView result = new ModelAndView("user"); ConsumerEntity consumerEntity = new ConsumerEntity(); consumerEntity.setId(1); consumerEntity.setFirstName("John"); consumerEntity.setLastName("Doe"); consumerEntity.setCity("Tokio"); consumerEntity.setZipCode("2000"); consumerEntity.setStreet("Oykayong 20."); ConsumerBo consumerBo = conversionService.convert(consumerEntity, ConsumerBo.class); result.addObject("user", consumerBo); return result; } }
Download source
You can download the source code of a working example here.