这是我的用例:
我有4个A,B,C,D类
我想克隆A类,然后递归地将id设置为null。
这里是一个示例:
public class ClassA {
private Long id;
private String name;
private boolean ok;
private ClassB classB;
private List<ClassC> classCList;
}
public class ClassB {
private Long id;
private String name;
private ClassD classD;
}
public class ClassC{
private Long id;
private String name;
}
public class ClassD{
private Long id;
private String name;
}
我开发了两个函数来实现该功能:
第一种方法:
public ClassA prepareClassA(ClassA detail) {
Optional.ofNullable(detail).ifPresent( detail -> {
detail.setId(null);
Optional.ofNullable(detail).map(ClassA::getClassB)
.ifPresent(objectB -> objectB.setId(null));
Optional.ofNullable(detail).map(ClassA::getClassB).map(ClassB::getClassD)
.ifPresent(objectB -> objectB.setId(null));
Optional.ofNullable(detail).map(ClassA::getClassCList).
.ifPresent(items -> items.stream().forEach(item -> {
item.setId(null);
}));
}
}
第二种方法 :(包括dozerMapper)
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>net.sf.dozer</groupId>
<artifactId>dozer</artifactId>
<version>5.5.1</version>
</dependency>
我使用了DozerBeanMapper实现
public ClassA prepareClassA(ClassA detail) {
ClassA objectA = new ClassA();
DozerBeanMapper dozerBeanMapper = new DozerBeanMapper();
BeanMappingBuilder bean = beanMappingBuilder(ClassA.class);
dozerBeanMapper.addMapping(bean);
Optional.ofNullable(detail).ifPresent(detail -> dozerBeanMapper.map(detail, objectA));
return details;
}
public BeanMappingBuilder beanMappingBuilder(Class<?> source) {
return new BeanMappingBuilder() {
@Override
protected void configure() {
mapping(source, source,
TypeMappingOptions .wildcard(true)
//Here i have to do my work ?
//TypeMappingOptions.mapNull(true)
);
}
};
}
我想要这个结果:
ClassA testA = new ClassA();
//fill all the objects in objectA with id != null
ClassA testA_convert = prepareClassA(testA);
// testA_convert.getId() must be null
// testA_convert.getClassB().getId() must be null
// testA_convert.getClassB().getClassD().getId() must be null
// testA_convert.getClassCList().forEach( element -> element.getId() must be null
问题:
最诚挚的问候
答案 0 :(得分:1)
如果您已经克隆了对象并且想在没有NPE的情况下将id设置为null,那么可以为此创建一个帮助程序接口:
interface Nullify<T> {
void apply(T obj);
default <G> Nullify<T> andThen(Function<T, G> function, Nullify<G> nullify) {
return (T t) -> {
apply(t);
G g = function.apply(t);
if(g != null) {
nullify.apply(g);
}
};
}
}
和用法
Nullify<ClassB> bNull = b -> b.setId(null);
bNull = bNull.andThen(ClassB::getClassD, d -> d.setId(null));
Nullify<ClassA> aNull = a -> a.setId(null);
aNull.andThen(ClassA::getClassB, bNull)
.andThen(ClassA::getClassCList, classCList -> classCList.forEach(c -> c.setId(null)))
.apply(classAObject);
尽管最好将克隆/复制方法配置为忽略ID(例如MapStruct)
答案 1 :(得分:0)
是否存在任何可以解决我的问题的库?
是的,MapStruct可以选择忽略所需的属性。
例如,定义一个配置界面
@Mapper(componentModel = "spring")
public interface DomainDtoMapper {
@Mapping(source = "id", target = "id", ignore = true)
ClassA map(ClassA cla);
@Mapping(source = "id", target = "id", ignore = true)
ClassB map(ClassB clb);
//...
}
然后只需自动连线DomainDtoMapper
并调用该方法,它将检查所有映射规则并进行相应复制:
@Autowire DomainDtoMapper mapper;
//...
public ClassA prepareClassA(ClassA detail) {
return mapper.map(detail);
}
答案 2 :(得分:0)
我认为我找到了一种简单的方法:
实施Gson库:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
并添加
public class ClassA {
private Long id;
@Expose
private String name;
@Expose
private boolean ok;
@Expose
private ClassB classB;
@Expose
private List<ClassC> classCList;
}
public class ClassB {
private Long id;
@Expose
private String name;
@Expose
private ClassD classD;
}
我将@Expose放到所有字段中!= id
这是我的主类中的实现
import com.google.gson.annotations.Expose;
public ClassA prepareClassA(ClassA detail) {
Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
ClassA object_A = gson.fromJson(gson.toJson(detail),ClassA.class);
return object_A;
}
它有效。
但是,有一种方法可以将@Expose一次放入所有属性(不包括Id)的类定义中
示例:
@Expose( exclude ="id" )
public class ClassB {
private Long id;
private String name;
private ClassD classD;
}
有帮助吗?