Mastering Spring Boot 3 BeanWrapper: Real-World Cases & Code Walkthrough
This article explains how Spring Boot 3's BeanWrapper simplifies Java object property manipulation with detailed examples covering basic usage, map binding, nested objects, collections, custom type conversion, HTTP request binding, property access, and metadata extraction.
1. Introduction
In Java development, handling object properties (getting, setting, querying) is common but often requires repetitive code. Spring's BeanWrapper class offers an elegant solution by abstracting property access, supporting nested properties, batch setting, and metadata queries.
2. Practical Cases
2.1 Basic Usage
BeanWrapper instanceWrapper = new BeanWrapperImpl(User.class);
instanceWrapper.setPropertyValue("id", 666L);
instanceWrapper.setPropertyValue("name", "Pack");
Object instance = instanceWrapper.getWrappedInstance();
System.err.println(instance);2.2 Using Map to Bind Data
BeanWrapper instanceWrapper = new BeanWrapperImpl(User.class);
Map<String, Object> data = new HashMap<>();
data.put("id", "8888");
data.put("name", "Pack");
instanceWrapper.setPropertyValues(data);
Object instance = instanceWrapper.getWrappedInstance();
System.err.println(instance);2.3 Using PropertyValue to Bind Data
BeanWrapper instanceWrapper = ...;
MutablePropertyValues pvs = new MutablePropertyValues();
PropertyValue pv = new PropertyValue("id", "8888");
pvs.addPropertyValue(pv);
instanceWrapper.setPropertyValues(pvs);2.4 Binding Nested Objects
BeanWrapper instanceWrapper = ...;
Map<String, Object> data = new HashMap<>();
data.put("id", "8888");
data.put("name", "Pack");
data.put("address.province", "新疆");
instanceWrapper.setPropertyValues(data);
Object instance = instanceWrapper.getWrappedInstance();
System.err.println(instance);2.5 Collection Property Binding
public class User {
// ...
private List<String> hobby = new ArrayList<>();
}
BeanWrapper instanceWrapper = ...;
Map<String, Object> data = ...;
data.put("hobby[0]", "足球");
data.put("hobby[1]", "篮球");
instanceWrapper.setPropertyValues(data);
Object instance = instanceWrapper.getWrappedInstance();
System.err.println(instance);2.6 Data Type Conversion
By default, BeanWrapper cannot convert enum values automatically. A custom converter can be registered:
public class GenderConverter implements Converter<String, GenderEnum> {
@Override
public GenderEnum convert(String source) {
if (source == null) return null;
try {
int code = Integer.parseInt(source);
return GenderEnum.fromCode(code);
} catch (Exception e) {
return null;
}
}
}
BeanWrapper instanceWrapper = new BeanWrapperImpl(User.class);
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
conversionService.addConverter(new GenderConverter());
instanceWrapper.setConversionService(conversionService);
Map<String, Object> data = new HashMap<>();
data.put("name", "Pack");
data.put("gender", "1");
instanceWrapper.setPropertyValues(data);
Object instance = instanceWrapper.getWrappedInstance();
System.err.println(instance);2.7 Binding HTTP Request Parameters
// Simulate ServletRequest
MockHttpServletRequest request = new MockHttpServletRequest();
request.setParameter("id", "888");
request.setParameter("name", "Spring Boot3实战案例200讲");
request.setParameter("hobby[0]", "足球");
request.setParameter("hobby[1]", "篮球");
BeanWrapper instanceWrapper = ...;
PropertyValues pvs = new ServletRequestParameterPropertyValues(request);
instanceWrapper.setPropertyValues(pvs);
Object instance = instanceWrapper.getWrappedInstance();
System.err.println(instance);2.8 Accessing Property Values
Properties can be read using BeanWrapper#getPropertyValue and its overloads. Common expressions:
Expression
Explanation name Corresponds to getName() or isName() and setName(..). account.name Nested property, equivalent to getAccount().getName() or setName(..) on the nested object. accounts[2] Indexed property, the third element of a list/array named accounts. accounts[KEY] Key‑based index, maps a key to a value in a collection.
2.9 Getting Property Metadata
For dynamic analysis (e.g., form generation, API docs), BeanWrapper can expose PropertyDescriptor objects:
BeanWrapper wrapper = new BeanWrapperImpl(User.class);
Arrays.asList(wrapper.getPropertyDescriptors()).forEach(pd -> {
System.err.println("name: " + pd.getName() + ", type: " + pd.getPropertyType()
+ ",
readMethod: " + pd.getReadMethod()
+ ",
writeMethod: " + pd.getWriteMethod());
System.out.println("-------------------------------");
});Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Spring Full-Stack Practical Cases
Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
