在我的项目中,我使用骆驼推土机将MyObject映射到MyObjectDTO。 Spring存储库返回Page<MyObject>
。我需要将Page<MyObject>
映射到Page<MyObjectDTO>
。是否存在无需迭代Page.content并为每个项目进行转换的映射的好方法?
我这样映射单个对象:
dozerBeanMapping.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<mappings xmlns="http://dozermapper.github.io/schema/bean-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://dozermapper.github.io/schema/bean-mapping http://dozermapper.github.io/schema/bean-mapping.xsd">
<mapping>
<class-a>ru.test.MyObject</class-a>
<class-b>ru.test.MyObjectDTO</class-b>
<field>
<a>myField</a>
<b>testField</b>
</field>
</mapping>
</mappings>
RestRoute.java
rest("/search").description("")
.get("/{id}")
.consumes("application/json").to("direct:getMyObjectByID")
from("direct:getMyObjectByID")
.routeId("direct:getMyObjectByID")
.bean("myObjectRepository", "getMyObjectById(${header.id})")
.to("dozer:transformMyObjectDTO?mappingFile=dozerBeanMapping.xml&targetModel=ru.test.MyObjectDTO")
答案 0 :(得分:0)
我的临时解决方案:立即从Spring Data存储库返回DTO列表。
@Repository("myObjectRepository")
public interface MyObjectRepository extends CrudRepository<MyObject, Integer> {
@Query(value = "SELECT new ru.test.MyObjectDTO(l.id, l.otherfields)" +
" from MyObject l where l.otherObject=:otherObject")
Page<MyObjectDTO> getAllByOtherObject(@Param("otherObject") OtherObject otherObject, Pageable pageable);
}