[英]Convert Complex Entity to DTO With ModelMapper
i'm working in a rest API using Spring boot.我在 rest API 使用 Spring 启动。 when i wanted to return my entity from an End Point i realized that the Properties are different from what i need on my response so i tried to use Model Mapper to return a DTO.当我想从端点返回我的实体时,我意识到属性与我在响应中需要的不同,所以我尝试使用 Model Mapper 返回 DTO。
My entity is like this:我的实体是这样的:
public class RuleEntity {
private String ruleId;
private String bankDecision;
private String aggregatorFunctionType;
private String limitOperatorType;
private double limitRule;
private Integer windowMinutes;
private Integer layer;
private String expressionRule;
private String status;
private List<GroupingKeyName> groupingKeyNames;
private List<RuleFilter> ruleFilters;
} }
And the DTO that i need Must Be Like this:我需要的 DTO 必须是这样的:
public class RuleDTO {
private String ruleId;
private String bankDecision;
private String aggregatorFunctionType;
private String limitOperatorType;
private double limitRule;
private Integer windowMinutes;
private Integer layer;
private String expressionRule;
private String status;
private List<String> groupingKeyNames;
private List<String> ruleFilters;
} }
The only change is that the last two lists are of String instead of The Object唯一的变化是最后两个列表是 String 而不是 Object
The Objects groupingKeyNames and ruleFilters have a Name and an ID, and i only need the name on the list of DTO so it is a List of Strings对象 groupingKeyNames 和 ruleFilters 有一个名称和一个 ID,我只需要 DTO 列表中的名称,所以它是一个字符串列表
I tried using Model Mapper like this:我试过像这样使用 Model Mapper:
ModelMapper modelMapper = new ModelMapper();
RuleSetModel ruleSetModel = modelMapper.map(ruleSetEntity, RuleSetModel.class);
it works, with all the properties but in the Lists it is returning something like:它适用于所有属性,但在列表中它返回如下内容:
groupingKeyNames=[GroupingKeyName(groupingKeyId=1, name=cardHash)], ruleFilters=[RuleFilter(ruleFilterId=1, name=status)]
What could i do so i get a result like this:我该怎么做才能得到这样的结果:
groupingKeyNames=[cardHash], ruleFilters=[status]
Thanks in advance!提前致谢!
Create a method into your RuleEntity to do it在您的 RuleEntity 中创建一个方法来执行此操作
public RuleDTO dto() {
// config to skip
PropertyMap<RuleEntity, RuleDTO> propertyMap = new PropertyMap<RuleEntity, RuleDTO>() {
@Override
protected void configure() {
skip(destination.getGroupingKeyNames());
skip(destination.getRuleFilters());
}
};
RuleDTO ruleDTO = new RuleDTO();
ModelMapper modelMapper = new ModelMapper();
modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
modelMapper.addMappings(propertyMap);
modelMapper.map(this,ruleDTO);
if (!this.groupingKeyNames.isEmpty()) {
ruleDTO.getGroupingKeyNames().clear();
List<String> tmpGroupingKeyNames = new ArrayList<>();
this.getGroupingKeyNames().forEach(itemDTO -> {
tmpGroupingKeyNames.add(itemDTO.name);
});
ruleDTO.getGroupingKeyNames().addAll(tmpGroupingKeyNames);
}
if (!this.ruleFilters.isEmpty()) {
ruleDTO.getRuleFilters().clear();
List<String> tmpRuleFilters = new ArrayList<>();
this.getRuleFilters().forEach(itemDTO -> {
tmpRuleFilters.add(itemDTO.name);
});
ruleDTO.getRuleFilters().addAll(tmpRuleFilters);
}
return ruleDTO;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.