Map to Object
MapStructPlus provides more powerful function for Map<String, Object> to object
Usage
Just add the @AutoMapMapper annotation to the target class when you want to automatically generate the interface and implementation classes that Map<String, Object> to the target class.
The supported value type
StringBigDecimalBigIntegerIntegerLongDoubleBooleanDateLocalDateTimeLocalDateLocalTimeURIURLCalendarCurrencyCustom classes(custom classes also require @AutoMapMapper annotation)
Transformation logic
For an attribute in the target class, it first determintes whether the key exists in the Map. If it does, it first determines the type, the conversion is attempted to the target type based on the type conversion tool provided by Hutool
It also supports internally nested Map<String, Object attributes to internally nested custom type attributes.
Example
- Define two class:
MapModelA和MapModelB
@AutoMapMapper
@Data
public class MapModelA {
private String str;
private int i1;
private Long l2;
private MapModelB mapModelB;
}
@AutoMapMapper
@Data
public class MapModelB {
private Date date;
}
- Test
@SpringBootTest
public class QuickStartTest {
@Autowired
private Converter converter;
@Test
public void test() {
Map<String, Object> mapModel1 = new HashMap<>();
mapModel1.put("str", "1jkf1ijkj3f");
mapModel1.put("i1", 111);
mapModel1.put("l2", 11231);
Map<String, Object> mapModel2 = new HashMap<>();
mapModel2.put("date", DateUtil.parse("2023-02-23 01:03:23"));
mapModel1.put("mapModelB", mapModel2);
final MapModelA mapModelA = converter.convert(mapModel1, MapModelA.class);
System.out.println(mapModelA); // MapModelA(str=1jkf1ijkj3f, i1=111, l2=11231, mapModelB=MapModelB(date=2023-02-23 01:03:23))
}
}
Custom Converter
Since 1.5.2, the type converter of @AutoMapMapper can be fully customized to decouple from hutool. Three-level priority is supported, from highest to lowest:
1. Class-level Override — @AutoMapMapper(use = ...)
Specify a custom converter for a single class:
public class MyConverter implements MapObjectConverter {
@Override
public String objToString(Object obj) {
// custom implementation
}
// ... implement remaining methods
}
@AutoMapMapper(use = MyConverter.class)
public class MyModel {
// ...
}
2. Global Default — @MapperConfig(mapObjectConverter = ...)
Configure once, all @AutoMapMapper classes inherit it (also configurable via compiler argument -Amapstruct.plus.mapObjectConverter=com.example.MyConverter):
@MapperConfig(mapObjectConverter = MyConverter.class)
public class MapStructPlusConfiguration {
}
3. Built-in Default
When no converter is specified, falls back to HutoolMapObjectConverter (requires hutool-core), identical to previous versions.
TIP
Custom converters must implement the MapObjectConverter interface and provide a public no-arg constructor. When using a custom converter, the hutool-core dependency is not required.
WARNING
When using the Spring/Spring Boot component model (default), custom converters must be registered as Spring Beans (e.g., add @Component annotation). The built-in HutoolMapObjectConverter is auto-registered by the framework.