我有一个包含许多子实体的父类。 此父类有2个实例。 想要将一个实例的数据复制到另一个实例(忽略所有子实体中的'id'属性)
----为了简洁起见,隐藏了吸气剂和塞特剂
public class IdBean {
private Long id;
}
public class City extends IdBean {
private String name;
}
public class Country extends IdBean {
private String name;
private List<City> cities;
}
public class Student extends IdBean {
private String name;
}
public class School extends IdBean {
private String name;
private List<Student> students;
private List<Country> countries;
}
@MapperConfig(mappingInheritanceStrategy = MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG)
public interface SchoolCentralConfig {
@Mapping(ignore = true, target = "id")
IdBean updateBeanEntityFromDto(IdBean dto);
}
@Mapper(config = SchoolCentralConfig.class)
public interface SchoolMapper {
SchoolMapper INSTANCE = Mappers.getMapper( SchoolMapper.class );
@Mapping(target = "companies.id", ignore = true )
void updateSchoolFromDto(School schoolDTO, @MappingTarget School schoolEntity);
}
我想忽略所有嵌套字段中的所有'id'属性。
答案 0 :(得分:0)
var settings = new JsonSerializerSettings
{
ContractResolver = new ExcludeObsoletePropertiesResolver()
};
var json = JsonConvert.SerializeObject(foo, settings);
尚不适用于嵌套方法。
您可以做的是同时为MappingInheritanceStrategy.AUTO_INHERIT_FROM_CONFIG
和Country
写一个签名。然后它将起作用。
所以:
Student