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

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

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&lt;GroupingKeyName&gt; groupingKeyNames;
private List&lt;RuleFilter&gt; 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&lt;String&gt; groupingKeyNames;
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:

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&lt;RuleEntity, RuleDTO&gt; propertyMap = new PropertyMap&lt;RuleEntity, RuleDTO&gt;() {
		@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&lt;String&gt; tmpGroupingKeyNames = new ArrayList&lt;&gt;();
		this.getGroupingKeyNames().forEach(itemDTO -&gt; {
			tmpGroupingKeyNames.add(itemDTO.name);
		});
		ruleDTO.getGroupingKeyNames().addAll(tmpGroupingKeyNames);
	}

	if (!this.ruleFilters.isEmpty()) {
		ruleDTO.getRuleFilters().clear();
		List&lt;String&gt; tmpRuleFilters = new ArrayList&lt;&gt;();
		this.getRuleFilters().forEach(itemDTO -&gt; {
			tmpRuleFilters.add(itemDTO.name);
		});
		ruleDTO.getRuleFilters().addAll(tmpRuleFilters);
	}
	return ruleDTO;
}

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:

确定