我在项目中使用camel-dozer将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")
最佳答案
我临时的可怕解决方案:立即从 Spring 数据存储库返回 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);
}
关于java - 将 MyObject 的页面映射到 MyObjectDTO 的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56661743/