@FieldMapper
A simple java solution for field mapping
An Annotation that can be used to copy properties from one object to another object with intermediate conversions.
The problem field mapper solves is eliminating multiple lines of code for converting one object type to another object type with intermediate conversion.
Use cases can be like, When there is need to convert entity to dto, Where some field type should be converted to another type.
Let’s say we have an entity User and a dto UserDto like below,
User.java:
public class User{
private Integer userId;
private String userName;
private Date createdDate;
private String active;
//Getter and Setters
}
UserDto.java:
public class UserDto{
private Integer userId;
private String userName;
private String createdDate;
private boolean isActive;
//Getter and Setters
}
Now, The need is to copy properties from User object to UserDto object.
What we typically do?
- For same data type we will use getter and setters to get value form
Userand set it toUserDtoobject. - When we need to set
createdDateinUserDto, we will use conversion method to convert date object fromUserobject to MM-DD-YY formated string. method to convert will be inDateUtilty.classfor reusing purpose. - For
isActivefield inUserDtowe need to convert Y/N to true/false. we can create conversion method in any class, let’s sayCommonUtilityand use that to convert to true/false and set the value toUserDto.
let’s look at conversion code below,
private UserDto userToUserDto(User user){
UserDto userDto= new UserDto();
userDto.setUserId(user.getUserId());
userDto.setUserName(user.getUserName());
userDto.setCreatedDate( DateUtilty.dateToMmddyy(user.getCreatedDate()));
userDto.setIsActive(CommonUtility.booleanToYN(user.getActive()));
return userDto;
}
Yeah..! That’s easy..! Here we have only four fields and code looks okay. but what if we have many fields and some fields needs to be converted while copying to different object type?
Code will be long line of setters and getters and conversion methods
Let’s use @FieldMapper:
1)Add @FieldMapper annotation in required fields.
public class User{
@FieldMapper
private Integer userId;
@FieldMapper
private String userName;
@FieldMapper(method="mmddyyToDate", clazz=DateUtilty.class)
private Date createdDate;
@FieldMapper(name="active" method="booleanToYn", clazz=CommonUtility.class)
private String active;
//Getter and Setters
}
public class UserDto{
@FieldMapper
private Integer userId;
@FieldMapper
private String userName;
@FieldMapper(method="dateToMmddyy", clazz=DateUtilty.class)
private String createdDate;
@FieldMapper(name="active" method="ynToBoolean", clazz=CommonUtility.class)
private boolean isActive;
//Getter and Setters
}
Let’s modify our userToUserDto method:
private UserDto userToUserDto(User user){
return FieldMapperUtility.copy(user,UserDto.class);
}
How to configure?
Just annotate the fields that needs to be copied. we have three usefull attributes in @FieldMapper
clazz– class form which the method should be accessedmethod– method name which is to be invoked from class mentioned in clazz-
name– name is to map two fields in two objects having different field name. if object from field and object to field have same name then this attribute can be skipped. In above example we have used name attribute to mapactivefield inUser.classandisActivefield inUserDto.class
What are the constraints??
- Method mentioned in
methodattribute of@FieldMappershould be static method - If Java Security Manager restricts permission to access field using java reflection API. This annotation cannot be used. Since
FieldMapperUtility.classuses java reflection API to access private members to get and set values
So Thats it ???
No..! More logic will be implemented. Samples and docs will be updated…!
Click here to redirect to GitHub repository
Useful information. Thanks @Daniel
LikeLiked by 1 person
Thank you
LikeLike
I used this annotation in my application. It is minimizing lot of effort.
LikeLiked by 1 person
Thank you. If you face any issues, Please reach out.
LikeLike