使用支持多态的Jackson和Hibernate,我想找到一个很好的Java映射库,它可以帮助我分离我的不同层(主要是DTO / Entities),并且还支持多态性。
Orika似乎支持多态(但不是没有类型的声明)。 https://orika-mapper.github.io/orika-docs/
我没有为其他两个竞争者Selma和MapStruct找到好的例子。 http://www.selma-java.org/ http://mapstruct.org/
有没有人有这些图书馆的经验?
更新1: 其中一个测试案例。
public class PolymorphicMapperTest {
public abstract class Animal{
private String fooAnimal;
public Animal(String fooAnimal) {
this.fooAnimal = fooAnimal;
}
public String getFooAnimal() {
return fooAnimal;
}
}
public class Cat extends Animal {
private String fooCat;
public Cat(String fooAnimal, String fooCat) {
super(fooAnimal);
this.fooCat = fooCat;
}
}
public class Snake extends Animal {
private String fooSnake;
public Snake(String fooAnimal, String fooSnake) {
super(fooAnimal);
this.fooSnake = fooSnake;
}
public String getFooSnake() {
return fooSnake;
}
}
public abstract class AnimalDTO{
public String fooAnimal;
}
public class CatDTO extends AnimalDTO {
public String fooCat;
}
public class SnakeDTO extends AnimalDTO {
public String fooSnake;
}
@Test
public void testMapping() {
Mapper mapper = MyAwesomeMapper();
mapper.declare(Animal.class,AnimalDTO.class);
SnakeDTO snakeDTO = new SnakeDTO();
snakeDTO.fooAnimal = "Snake";
snakeDTO.fooSnake = "Anaconda";
//I don't want the mapper to know the input Type because Jackson will hide it to me.
AnimalDTO animalDTOToMap = (AnimalDTO) snakeDTO;
Animal animal1 = mapper.map(animalDTOToMap);
Assert.isTrue(animal1 instanceof Snake);
//And when casted we want to find Animal and Snake fields mapped
CatDTO catDTO = new CatDTO();
catDTO.fooAnimal = "Cat";
catDTO.fooCat = "Persan";
AnimalDTO secondAnimalDTOToMap = (AnimalDTO) catDTO;
Animal animal2 = mapper.map(secondAnimalDTOToMap);
Assert.isTrue(animal2 instanceof Cat);
//And when casted we want to find Animal and Cat fields mapped
}
}
答案 0 :(得分:-1)