将复杂实体转换为DTO,使用ModelMapper。

huangapple go评论95阅读模式
英文:

Convert Complex Entity to DTO With ModelMapper

问题

我正在使用Spring Boot开发一个REST API。当我想要从一个端点返回我的实体时,我意识到属性与我在响应中所需的不同,所以我尝试使用Model Mapper来返回一个DTO。

我的实体是这样的:

  1. public class RuleEntity {
  2. private String ruleId;
  3. private String bankDecision;
  4. private String aggregatorFunctionType;
  5. private String limitOperatorType;
  6. private double limitRule;
  7. private Integer windowMinutes;
  8. private Integer layer;
  9. private String expressionRule;
  10. private String status;
  11. private List<GroupingKeyName> groupingKeyNames;
  12. private List<RuleFilter> ruleFilters;
  13. }

而我需要的DTO如下:

  1. public class RuleDTO {
  2. private String ruleId;
  3. private String bankDecision;
  4. private String aggregatorFunctionType;
  5. private String limitOperatorType;
  6. private double limitRule;
  7. private Integer windowMinutes;
  8. private Integer layer;
  9. private String expressionRule;
  10. private String status;
  11. private List<String> groupingKeyNames;
  12. private List<String> ruleFilters;
  13. }

唯一的变化是最后两个列表中的对象是字符串,而不是对象。

groupingKeyNames和ruleFilters对象具有名称和ID,我只需要在DTO的列表中使用名称,因此它是一个字符串列表。

我尝试使用以下方式使用Model Mapper:

  1. ModelMapper modelMapper = new ModelMapper();
  2. RuleDTO ruleDTO = modelMapper.map(ruleEntity, RuleDTO.class);

这可以工作,可以处理所有的属性,但在列表中,它返回类似于以下的内容:

  1. groupingKeyNames=[GroupingKeyName(groupingKeyId=1, name=cardHash)], ruleFilters=[RuleFilter(ruleFilterId=1, name=status)]

我应该怎么做才能获得以下结果:

  1. 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:

  1. public class RuleEntity {
  2. private String ruleId;
  3. private String bankDecision;
  4. private String aggregatorFunctionType;
  5. private String limitOperatorType;
  6. private double limitRule;
  7. private Integer windowMinutes;
  8. private Integer layer;
  9. private String expressionRule;
  10. private String status;
  11. private List&lt;GroupingKeyName&gt; groupingKeyNames;
  12. private List&lt;RuleFilter&gt; ruleFilters;

}

And the DTO that i need Must Be Like this:

  1. public class RuleDTO {
  2. private String ruleId;
  3. private String bankDecision;
  4. private String aggregatorFunctionType;
  5. private String limitOperatorType;
  6. private double limitRule;
  7. private Integer windowMinutes;
  8. private Integer layer;
  9. private String expressionRule;
  10. private String status;
  11. private List&lt;String&gt; groupingKeyNames;
  12. private List&lt;String&gt; 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:

  1. ModelMapper modelMapper = new ModelMapper();
  2. RuleSetModel ruleSetModel = modelMapper.map(ruleSetEntity, RuleSetModel.class);

it works, with all the properties but in the Lists it is returning something like:

  1. groupingKeyNames=[GroupingKeyName(groupingKeyId=1, name=cardHash)], ruleFilters=[RuleFilter(ruleFilterId=1, name=status)]

What could i do so i get a result like this:

  1. groupingKeyNames=[cardHash], ruleFilters=[status]

Thanks in advance!

答案1

得分: 1

  1. // 在你的 RuleEntity 中创建一个方法来完成这个操作
  2. public RuleDTO dto() {
  3. // 配置以跳过某些属性
  4. PropertyMap<RuleEntity, RuleDTO> propertyMap = new PropertyMap<RuleEntity, RuleDTO>() {
  5. @Override
  6. protected void configure() {
  7. skip(destination.getGroupingKeyNames());
  8. skip(destination.getRuleFilters());
  9. }
  10. };
  11. RuleDTO ruleDTO = new RuleDTO();
  12. ModelMapper modelMapper = new ModelMapper();
  13. modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
  14. modelMapper.addMappings(propertyMap);
  15. modelMapper.map(this, ruleDTO);
  16. if (!this.groupingKeyNames.isEmpty()) {
  17. ruleDTO.getGroupingKeyNames().clear();
  18. List<String> tmpGroupingKeyNames = new ArrayList<>();
  19. this.getGroupingKeyNames().forEach(itemDTO -> {
  20. tmpGroupingKeyNames.add(itemDTO.name);
  21. });
  22. ruleDTO.getGroupingKeyNames().addAll(tmpGroupingKeyNames);
  23. }
  24. if (!this.ruleFilters.isEmpty()) {
  25. ruleDTO.getRuleFilters().clear();
  26. List<String> tmpRuleFilters = new ArrayList<>();
  27. this.getRuleFilters().forEach(itemDTO -> {
  28. tmpRuleFilters.add(itemDTO.name);
  29. });
  30. ruleDTO.getRuleFilters().addAll(tmpRuleFilters);
  31. }
  32. return ruleDTO;
  33. }
英文:

Create a method into your RuleEntity to do it

  1. public RuleDTO dto() {
  2. // config to skip
  3. PropertyMap&lt;RuleEntity, RuleDTO&gt; propertyMap = new PropertyMap&lt;RuleEntity, RuleDTO&gt;() {
  4. @Override
  5. protected void configure() {
  6. skip(destination.getGroupingKeyNames());
  7. skip(destination.getRuleFilters());
  8. }
  9. };
  10. RuleDTO ruleDTO = new RuleDTO();
  11. ModelMapper modelMapper = new ModelMapper();
  12. modelMapper.getConfiguration().setPropertyCondition(Conditions.isNotNull());
  13. modelMapper.addMappings(propertyMap);
  14. modelMapper.map(this,ruleDTO);
  15. if (!this.groupingKeyNames.isEmpty()) {
  16. ruleDTO.getGroupingKeyNames().clear();
  17. List&lt;String&gt; tmpGroupingKeyNames = new ArrayList&lt;&gt;();
  18. this.getGroupingKeyNames().forEach(itemDTO -&gt; {
  19. tmpGroupingKeyNames.add(itemDTO.name);
  20. });
  21. ruleDTO.getGroupingKeyNames().addAll(tmpGroupingKeyNames);
  22. }
  23. if (!this.ruleFilters.isEmpty()) {
  24. ruleDTO.getRuleFilters().clear();
  25. List&lt;String&gt; tmpRuleFilters = new ArrayList&lt;&gt;();
  26. this.getRuleFilters().forEach(itemDTO -&gt; {
  27. tmpRuleFilters.add(itemDTO.name);
  28. });
  29. ruleDTO.getRuleFilters().addAll(tmpRuleFilters);
  30. }
  31. return ruleDTO;
  32. }

huangapple
  • 本文由 发表于 2020年5月29日 04:42:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/62074173.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定