any tool for java object to object mapping?
You could try Dozer.
Dozer is a Java Bean to Java Bean
mapper that recursively copies data
from one object to another. Typically,
these Java Beans will be of different
complex types.Dozer supports simple property
mapping, complex type mapping,
bi-directional mapping,
implicit-explicit mapping, as well as
recursive mapping. This includes
mapping collection attributes that
also need mapping at the element
level.
How to Map specific fields of an object to another object?
You can use ModelMapper. This library is used to perform this entity-DTO conversion.
- Add below maven dependancy :
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>0.7.4</version>
</dependency>
- Autowire the ModelMapper -
@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
- Sample Code transforming DTO to entity :
private Employee convertToDto(EmployeeDto employeeDto) {
Employee employee = modelMapper.map(employeeDto, Employee.class);
return employee;
}
Object in Object to HashMap with reflection
You need to decide if a value should me put plain in the map or recursively. Therefore you can use a simple method like this:
private boolean isSimpleType(Class<?> type) {
return type.isPrimitive() ||
Boolean.class == type ||
Character.class == type ||
CharSequence.class.isAssignableFrom(type) ||
Number.class.isAssignableFrom(type) ||
Enum.class.isAssignableFrom(type);
}
This method returns true
for all primitive types or the Wrapper objects, Strings and Enums. You can simply adjust that methods to fit your needs. Look here for more details on how to determine if a class is simple.
Now you can use to transform an object:
public Map<String, Object> convert(Object object) {
Map<String, Object> map = new HashMap<>();
for (Field field : object.getClass().getDeclaredFields()) {
field.setAccessible(true);
try {
if (isSimpleType(field.getType())) {
map.put(field.getName(), field.get(object));
} else {
map.put(field.getName(), convert(field.get(object)));
}
} catch (Exception e) {
e.printStackTrace();
}
}
return map;
}
You also can adjust this to do fir your export
method.
The result for your example will be this:
{a=2, b=5, bar={q=10, e=it works, w=15}, c=abc}
Beside that I would strongly recommend using an library (like Jackson), which already does stuff like this by perfection. Here is the same solution using Jackson:
Map result = new ObjectMapper().convertValue(object, Map.class);
Or if you need type safety:
ObjectMapper mapper = new ObjectMapper();
MapType mapType = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
Map<String, Object> result = mapper.convertValue(value, mapType);
Simple object-to-object mapper without xml?
I'd prefer to do this in your Java code if possible. I'm not sure why there's a benefit to having some declaration-based solution when a code-based solution is more likely easier to read and more extensible.
If you need a framework to do this, perhaps Dozer is of use. It provides a means of identifying mappings using annotations (as well as XML)
How to convert a Java object (bean) to key-value pairs (and vice versa)?
There is always apache commons beanutils but of course it uses reflection under the hood
Related Topics
Tool for Creating a Java Daemon Service on Linux
Tomcat Doesn't Stop. How to Debug This
Problems with Java3D Lib Configuration
Java Is Installed, in Listing, But Execution Produces "./Java: No Such File or Directory"
Couldn't Install Netbeans 11.3 with Java 14 Due to Error: "Unsupported Jvm Version"
Java Exec() Does Not Return Expected Result of Pipes' Connected Commands
Java Jsch Changing User on Remote MAChine and Execute Command
Why This Javac: File Not Found Error
Tomcat 7: How to Set Initial Heap Size Correctly
How to Check If a File Is Open by Another Process (Java/Linux)
Why Does Tomcat Work with Port 8080 But Not 80
Difference Between Using Java.Library.Path and Ld_Library_Path
Setting/Changing the Ctime or "Change Time" Attribute on a File
How to Find My Pid in Java or Jruby on Linux
How to Get the Ip of the Computer on Linux Through Java