
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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 {
Logger LOGGER = LoggerFactory.getLogger(DefaultConvertDTO.class);
/**
* 转换
*
* @return DTO对象
*/
@Override
@JsonIgnore
@SuppressWarnings("unchecked")
default D toDTO() {
Type[] types = getClass().getGenericInterfaces();
Class clazz = null;
D dto = null;
for (Type type : types) {
if (!(type instanceof ParameterizedType)) {
continue;
}
ParameterizedType parameterizedType = (ParameterizedType) type;
Type rawType = parameterizedType.getRawType();
if (rawType == DefaultConvertDTO.class) {
clazz = (Class) parameterizedType.getActualTypeArguments()[0];
break;
}
}
try {
dto = clazz.newInstance();
} catch (Exception e) {
LOGGER.error(e.getLocalizedMessage(), e);
throw new ApplicationException(e.getLocalizedMessage(), e);
}
BeanUtils.copyProperties(this, dto);
return dto;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy