I'm trying to convert a List<String[]>
into List<Object>
using Dozer but unable to map the index values to the property fields using mapper API configuration .
How can I map the members of the String[]
into individual object fields with each index targeting a specific field? (eg [0] -> name
, and [1] -> role
)
DozerBeanMapper mapper = new DozerBeanMapper();
BeanMappingBuilder builder = new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(String[].class, User.class)
.fields(this_(), "name"); // HOW do I specify index?**
}
};
mapper.addMapping(builder);
List<String[]> users = new ArrayList<>();
String[] user1 = {"Jill", "SDE"};
String[] user2 = {"Jack", "PM"};
users.add(user1);
users.add(user2);
List<User> userList = mapObjects(mapper, users, User.class);
where mapObjects()
is;
private static <T1, T2> List<T2> mapObjects(DozerBeanMapper mapper, List<T1> sourceList, Class<T2> destinationClazz) {
try {
return sourceList.stream()
.map(i -> mapper.map(i, destinationClazz))
.collect(Collectors.toList());
} catch (Exception e) {
...
}
return new ArrayList<>();
}
and User
class;
class User {
String name;
String role;
// getter & setter
}
It worked perfectly with the following configuration;
DozerBeanMapper mapper = new DozerBeanMapper();
BeanMappingBuilder builder = new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(String[].class, User.class)
.fields(this_(), "name", FieldsMappingOptions.customConverterId("arrToName"))
.fields(this_(), "role", FieldsMappingOptions.customConverterId("arrToRole"));
}
};
final Map<String, CustomConverter> customConverterMap = new HashMap<>();
customConverterMap.put("arrToName", new ArrToNameConverter());
customConverterMap.put("arrToRole", new ArrToRoleConverter());
mapper.setCustomConvertersWithId(customConverterMap);
mapper.addMapping(builder);
Utilizing a logic where String[]
is mapped into name
and role
fields separately via custom converters, which are targeting a specific index of the input String[]
. With dozer
, you can essentially define custom converters and assign them an id, and refer them with those ids inside of field mappings FieldsMappingOptions.customConverterId("{id}")
where ArrToNameConverter
;
public class ArrToNameConverter extends DozerConverter<String[], String> {
public ArrToNameConverter() {
super(String[].class, String.class);
}
@Override
public String convertTo(String[] strings, String user) {
return strings[0];
}
@Override
public String[] convertFrom(String user, String[] strings) {
return new String[0];
}
}
and ArrToRoleConverter
;
public class ArrToRoleConverter extends DozerConverter<String[], String> {
public ArrToRoleConverter() {
super(String[].class, String.class);
}
@Override
public String convertTo(String[] strings, String user) {
return strings[1];
}
@Override
public String[] convertFrom(String user, String[] strings) {
return new String[0];
}
}
With the above mapper
, I was able to get the following result;
[User(name=Jill, role=SDE), User(name=Jack, role=PM)]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.