英文:
Convert Complex Entity to DTO With ModelMapper
问题
我正在使用Spring Boot开发一个REST API。当我想要从一个端点返回我的实体时,我意识到属性与我在响应中所需的不同,所以我尝试使用Model Mapper来返回一个DTO。
我的实体是这样的:
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;
}
而我需要的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;
}
唯一的变化是最后两个列表中的对象是字符串,而不是对象。
groupingKeyNames和ruleFilters对象具有名称和ID,我只需要在DTO的列表中使用名称,因此它是一个字符串列表。
我尝试使用以下方式使用Model Mapper:
ModelMapper modelMapper = new ModelMapper();
RuleDTO ruleDTO = modelMapper.map(ruleEntity, RuleDTO.class);
这可以工作,可以处理所有的属性,但在列表中,它返回类似于以下的内容:
groupingKeyNames=[GroupingKeyName(groupingKeyId=1, name=cardHash)], ruleFilters=[RuleFilter(ruleFilterId=1, name=status)]
我应该怎么做才能获得以下结果:
groupingKeyNames=[cardHash],ruleFilters=[status]
提前感谢您的帮助!
英文:
i'm working in a rest API using Spring boot.
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.
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:
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
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
I tried using Model Mapper like this:
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!
答案1
得分: 1
// 在你的 RuleEntity 中创建一个方法来完成这个操作
public RuleDTO dto() {
// 配置以跳过某些属性
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;
}
英文:
Create a method into your RuleEntity to do it
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论