January 28, 2021
Hot Topics:

The New Spring 3.0 Features You Need to Know

  • By Yuvarani Meiyappan
  • Send Email »
  • More Articles »

Type-Conversion System

Typically in a web-based enterprise application, you have to convert string inputs to the corresponding data type and convert the data type back to string when rendering the response. Spring 3.0 has introduced a general type-conversion system to make this process more efficient. The framework provides a converter SPI and API. The converter SPI allows you to implement type-conversion logic, while the API allows you to invoke and execute the type conversions logic provided at runtime.

Spring 3.0 delivers its type-conversion system via three packages:

  • org.springframework.core.convert – contains the type conversion API
  • org.springframework.core.convert.converter – contains the type conversion SPI
  • org.springframework.core.convert.support – contains some robust conversion service implementations

Converter SPI

The converter SPI has the following five interfaces:

  • Converter<S,T> – Implement this interface when you need to convert the source type S to target type T.
  • ConverterFactory<S,R> – Implement this interface when you need to convert a source type S to a class R in the class hierarchy.
  • GenericConverter – This is the most flexible but complex converter SPI interface. It supports conversion between multiple source and target type pairs. In addition, it has access to the source and target field context, enabling you to resolve field metadata such as annotations and generics information.
  • ConditionalGenericConverter – This subinterface of GenericConverter enables the GenericConverter only when a certain defined condition is satisfied. For example, the GenericConverter can be invoked only if the source type has the definition of a specific method.
  • ConverterRegistry – Use this interface to register a converter with the conversion system.

Here is a typical example of converting a string to an integer.

public class StringToInteger implements Converter<String, Integer> {
    public Integer convert(String source) {
        return Integer.valueOf(source);

Converter API

The converter API has the ConversionService interface and TypeDescriptor class. The Spring 3.0 conversion system implements the ConversionService interface from the converter API and the registry available in the Converter SPI. The ConversionService implementation delegates requests to the converter, which is registered with the registry, while the TypeDescriptor specifies the context of a type. The GenericConversionService, which implements the ConversionService and the registry classes, is a conversion service suitable for most environments.

Injecting the Converters

The converters created can be injected into the Spring container (ApplicationContext) or into the individual beans. The ConversionServiceFactoryBean factory class installs default converters that are appropriate for most environments. To override the converters, you can inject the converters dependency into ConversionServiceFactoryBean.

The following configuration (with a bean ID of conversionService) would inject the default ConversionService to the Spring container.

<bean id="conversionService" class="org.springframework.context.support.ConversionServiceFactoryBean" />

This code snippet accesses the conversionService to convert an integer to string.

ConversionService service = (ConversionService)context.
if (service.canConvert(Integer.class, String.class)){
             value = (String)service.convert(12.3, String.class);

You can configure the ConversionService programmatically as well, using the @Autowired annotation.

Formatter SPI and APIs

Spring 3.0 has also introduced a general-purpose type formatter system to format the response to be rendered back to the user.

Formatter SPI

The Spring 3.0 framework offers a separate Formatter SPI. The Formatter SPI can be used in client environments such as a web application that requires data to be parsed and localized.

The Formatter SPI contains the interface Formatter, which extends Printer and Parser. The Printer<T> interface would display the object of type <T>. The Parser<T> interface would produce the instances of T, thereby parsing the text.

Formatter API

The Formatter API has subpackages for datetime and number formatting. It also has a joda subpackage within datetime that supports the Joda types format. The number formatting subpackage has CurrencyFormatter, NumberFormatter, and PercentFormatter.

Annotation can be bound to the formatter by implementing AnnotationFormatterFactory.

Format Annotation API

The Format Annotation API has the subpackage annotation for annotation types. It supports DateTimeFormat and NumberFormat.

The following code snippet demonstrates the usage of the DateTimeFormat annotation.

    private Date date;

RESTful Web Services Support

REST is a compelling alternate to SOAP for web services. Spring 3.0 has added RESTful functionality to Spring MVC. It supports both server-side applications that provide the service and client-side web applications that consume the service using RESTful principles.

Providing Server-side RESTful Services

The @PathVariable annotation binds the method parameters to a URI template variable.

    public ModelAndView display(@PathVariable String uName){
        System.out.println("Hello " + uName);
        String msg = "Hello " + uName;
        return new ModelAndView("/WEB-INF/jsp/Success.jsp","msg",msg);

When the client raises a request to /welcome/John, the John will be bound to uName.

Accessing RESTful Services from the Client

The RestTemplate is the core class for client-side HTTP access. The consumer can invoke the RESTful web service using the RestTemplate class and HttpMessageConverter.

The following code invokes a RESTful web service.

String uri = "http://hostname/ApplicationContext/welcome/{uName}";
            RestTemplate template = new RestTemplate();
            template.getForObject(uri, String.class, "John");

The getForObject method retrieves the representation by performing GET on the specified URL. The RestTemplate also has support for other HTTP methods. The HttpMessageConverter converts the HTTP request and responses between the provider and the consumer.

Spring 3.0 supports various HttpMessageConverter implementations, such as MarshallingHttpMessageConverter for XML, StringHttpMessageConverter for text/plain content type, MappingJacksonHttpMessageConverter for JSON, Jaxb2RootElementHttpMessageConverter for XML using JAXB2, and others.

Spring 3.0 Honorable Mention

The following are some other noteworthy features and enhancements in Spring 3.0:

  • Spring 3.0 supports the JMS integration framework, which simplifies the use of the JMS API within the application.
  • Spring 3.0 includes Spring Portlet (part of the Spring Web component).
  • Spring supports integration with the Struts web tier, but Spring 3.0 has deprecated its support for Struts 1.x. However, you can replace it with Struts 2.0 and its Spring integration.
  • Spring 3.0 supports Java EE 6, with packages that enable Spring 3.0 applications to integrate with the Java Persistence API, JavaServer Faces 2.0, and Servlet 3.0. Spring 3.0 also provides support for asynchronous method invocation via the @Async annotation.
  • Spring AOP offers a separate Aspect module, which allows you to integrate Spring with AspectJ.
  • Spring 3.0 has deprecated the legacy JUnit 3.8 class hierarchy. Instead, it provides a separate module test that allows you to test components developed in Spring with JUnit and TestNG.


The author would like to thank Raghavendran N. and Sangeetha S. for their guidance, support, and constant encouragement.

About the Author

Yuvarani Meiyappan works as a lead in the E&R division of Infosys Technologies Limited. She has over eight years of experience in design and development of Java and Java EE applications.

Page 2 of 2

This article was originally published on January 30, 2010

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date