
net.guerlab.spring.commons.dto.DefaultConvertDTO Maven / Gradle / Ivy
package net.guerlab.spring.commons.dto;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import org.springframework.beans.BeanUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import net.guerlab.commons.exception.ApplicationException;
/**
* 转换为DTO对象
*
* @author guer
*
* @param
* DTO对象类型
*/
public interface DefaultConvertDTO extends ConvertDTO {
/**
* 转换
*
* @return DTO对象
*/
@Override
@JsonIgnore
@SuppressWarnings("unchecked")
default D toDTO() {
Type[] types = getClass().getGenericInterfaces();
Class clazz = null;
for (Type type : types) {
if (!(type instanceof ParameterizedType)) {
continue;
}
ParameterizedType parameterizedType = (ParameterizedType) type;
Type rawType = parameterizedType.getRawType();
if (DefaultConvertDTO.class.equals(rawType)) {
clazz = (Class) parameterizedType.getActualTypeArguments()[0];
break;
}
}
D dto;
try {
dto = clazz.newInstance();
} catch (Exception e) {
throw new ApplicationException(e.getLocalizedMessage(), e);
}
BeanUtils.copyProperties(this, dto);
return dto;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy