Spring property editor

This tutorial I will demonstrate how to create PropertyEditor to perform the conversion. First convert DateTime to String and print on the console. Example we have the following Agent class.


@Table(name = "agent")
public class Agent {

    @Column(name = "agent_id", length = 15)
    private String agentId;

    @Column(name = "agent_name", length = 50)
    private String agentName;

    @Column(name = "contact", length = 50)
    private String contact;

    @Column(name = "email", length = 50)
    private String email;

    @Column(name = "expired_date")
    private DateTime expiredDate;
    // .... code omitted 


1) Now’s register the agent bean in application context.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <bean id="dateTimeEditor" 
    	 <constructor-arg value="dd-MM-yyyy"/>

    <bean id="agentSmith" class="com.codeomitted.model.Agent" 
    	p:expiredDate="30-11-2013" />



1) Create a simple java application to print the agent object.

import com.codeomitted.model.Agent;

public class App {
    public static void main(String[] args) {

       GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();

       Agent agent = ctx.getBean("agentSmith", Agent.class);

       System.out.println("agent id : " + agent.getAgentId());
       System.out.println("agent name : " + agent.getName());
       System.out.println("agent contact : " + agent.getContact());
       System.out.println("agent email : " + agent.getEmail());
       System.out.println("agent age : " + agent.getAge());
       System.out.println("agent expired : " + agent.getExpiredDate());



2) Now check the exception, Failed to convert property value of type String to required type DateTime.

Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type ‘java.lang.String’ to required type ‘org.joda.time.DateTime’ for property ‘permitExpiredDate’; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.joda.time.DateTime] for property ‘permitExpiredDate’: no matching editors or conversion strategy found


3) We need  a propertyEditor to convert the DateTime to String.

public class DateTimeEditor extends PropertyEditorSupport {

    private DateTimeFormatter formatter;

    public DateTimeEditor(String pattern) {	
        formatter = DateTimeFormat.forPattern(pattern);

    public void setAsText(String text) throws IllegalArgumentException {		
        setValue(DateTime.parse(text, formatter));


4) Execute again, the result can be print as string.

agent id : mba123
agent name : wilsmith
agent contact : smith
agent email : agentsmith@loremipsum.com
agent age : 30
agent expired : 2013-11-30T00:00:00.000+08:00


Spring property editor

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.