【发布时间】:2018-11-25 10:26:56
【问题描述】:
我为我的 Spring Data Repository 创建了一个基于类的 Projection。这很好用。然后我尝试用 QueryDSL 中的@QueryProjection 对构造函数进行注解,希望得到一个具有分页、排序和过滤功能的 REST Endpoint。
代码如下所示,但为简洁起见,省略了更多字段和细节:
实体:
@Data
@Entity
public class Entity extends BaseEntity {
private String fieldA, fieldB;
private AnotherEntity ae;
}
DTO:
@Getter
@FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE)
public class EntityDto {
private final String fieldA;
private final String anotherEntityFieldC;
@QueryProjection
public EntityDto(final String fieldA, final String anotherEntityFieldC) {
this.fieldA = fieldA;
this.anotherEntityFieldC = anotherEntityFieldC;
}
}
存储库:
public EntityRepo extends JpaRepository<Entity, Long>, QuerydslBinderCustomizer<EntityPath<Entity>>, QuerydslPredicateExecutor<Entity> {
Page<EntityDto> findPageProjectedBy(Predicate predicate, Pageable pageable);
}
端点:
@RestController
@RequestMapping(EntityEndpoint.ROOT)
@RequiredArgsConstructor(onConstructor = @__({@Autowired}))
public EntityEndpoint {
private final EntityRepo er;
@GetMapping
public ResponseEntity<PagedResources<Resource<EntityDto>>> getAllEntities(
Pageable pageable,
@QuerydslPredicate(root = EntityDto@.class) Predicate predicate,
PagedResourcesAssembler<EntityDto> assembler) {
Page<EntityDto> page = er.findPageProjectedBy(predicate, pageable);
return new ResponseEntity<>(assembler.toResource(page), HttpStatus.OK);
}
}
我得到了例外:
java.lang.IllegalStateException: 在类 at.dataphone.logis4.model.dto.QBuchungDto 中没有找到相同类型的静态字段!
这就是网址:
curl -X GET --header 'Accept: application/json' 'http://localhost:8080/Entity'
【问题讨论】:
标签: spring rest spring-data-jpa spring-data querydsl