Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Mastering Spring PropertyEditor: Custom Data Binding in Spring Boot

Mastering Spring PropertyEditor: Custom Data Binding in Spring Boot

This tutorial explains how to use Java's PropertyEditor and PropertyEditorManager for custom String-to-object conversion, demonstrates registration in both non‑web and web Spring Boot environments, and provides practical code examples for seamless data binding.

1. Introduction

In a previous article we covered Spring data type conversion; this guide focuses on another way to achieve data binding using the PropertyEditor concept, which converts between String and custom objects such as dates.

2. PropertyEditor Conversion

The java.beans.PropertyEditor classes are part of the JDK and can be used in any Java project without additional dependencies.

2.1 Prepare Beans

public class User {
  private Integer age;
  private String name;
  // getters, setters
}

public class Dto {
  private User user;
  // getters, setters
}

2.2 Implement PropertyEditor

public class UserPropertyEditor extends PropertyEditorSupport {
  @Override
  public void setAsText(String text) throws IllegalArgumentException {
    String[] dataStr = text.split(",");
    User user = new User();
    user.setAge(Integer.valueOf(dataStr[0]));
    user.setName(dataStr[1]);
    setValue(user);
  }
}

Usage example:

UserPropertyEditor editor = new UserPropertyEditor();
editor.setAsText("12,张三");
User user = editor.getValue();

Registering with PropertyEditorManager for global reuse:

PropertyEditorManager.registerEditor(User.class, UserPropertyEditor.class);
PropertyEditor editor = PropertyEditorManager.findEditor(User.class);
// beware of possible NPE
editor.setAsText("12,张三");

3. Integrating PropertyEditor with Spring

3.1 Non‑Web Environment

Define a BeanFactoryPostProcessor to register the custom editor:

public class EditorBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    beanFactory.registerCustomEditor(User.class, UserEditor1.class);
  }
}

Bind data manually:

// obtain BeanFactory instance
ConfigurableListableBeanFactory beanFactory;
Dto target = new Dto();
DataBinder binder = new DataBinder(target);
beanFactory.copyRegisteredEditorsTo(binder);
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("user", "22,张三");
binder.bind(pvs);

3.2 Web Environment

Method 1 – @InitBinder

@GetMapping("")
public Dto index(Dto dto) {
  return dto;
}

@InitBinder
public void init(WebDataBinder binder) {
  binder.registerCustomEditor(User.class, new UserPropertyEditor());
}

You can place the @InitBinder method inside a class annotated with @ControllerAdvice to apply it globally.

Method 2 – Naming Convention

Simply place the custom editor class in the same package as the target type and name it UserEditor. Spring will automatically discover it.

com
  pack
    domain
      User
      UserEditor

File name must follow the pattern {TargetClass}Editor, e.g., UserEditor for the User type.

File structure illustration
File structure illustration

Both the PropertyEditor and PropertyEditorManager mechanisms work in Spring environments, providing a flexible way to handle custom type conversions.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
Backend DevelopmentSpring Bootdata bindingPropertyEditor

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion