Spring Data REST:sort参数被忽略

时间:2019-02-08 14:18:48

标签: java spring spring-data-jpa spring-data spring-data-rest

带有Spring Data JPA和Spring Data REST的Spring Boot 2.0.1.RELEASE项目。似乎REST端点中的sort参数被忽略了(但是相同的存储库方法确实可以在单元测试中使用)。 存储库如下:

@RepositoryRestResource(collectionResourceRel = "orders", path = "orders")
public interface OrderRepository extends PagingAndSortingRepository<OrderEntity, Integer>, OrderRepositoryExtended {

@Query(value = "FROM OrderEntity a WHERE "
                + " (a.orderDateTime BETWEEN :dateFrom AND :dateTo) AND"
                + " ("
                + "  :searchTerm IS NULL OR"
                + "  (LOWER(a.customer.companyName) LIKE '%' || LOWER(:searchTerm) || '%') OR"
                + "  (LOWER(a.orderCode) LIKE '%' || LOWER(:searchTerm) || '%')"
                + " )"
)
Page<OrderEntity> findByOrderDateTimeBetweenAndSearchTerm(
        @RequestParam(name = "dateFrom")
        @Param("dateFrom")
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
            LocalDateTime dateFrom,

        @RequestParam(name = "dateTo")
        @Param("dateTo")
        @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
            LocalDateTime dateTo,

        @RequestParam(name = "searchTerm")
        @Param("searchTerm")
            String searchTerm,

        Pageable pageable);

}

实体(已编辑)如下:

@Entity
@Table(name = "T_ORDERS")
@Data @NoArgsConstructor
@Cacheable(false)
public class OrderEntity {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO, generator = "native")
    @GenericGenerator(name = "native", strategy = "native")
    @Column(name = "ID_ORDER")
    @JsonProperty("id_order")
    private int id;

    @Column(name = "DATE_ORDER")
    @JsonProperty("date_order")
    private LocalDateTime orderDateTime;

}

当我尝试使用导出的REST端点调用它时,例如:

http://localhost:8080/api/v1/orders/search/findByOrderDateTimeBetweenAndSearchTerm?dateFrom=2018-01-01T00:00:00&dateTo=2018-12-01T00:00:00&sort=orderDateTime&searchTerm=O-2018

如您在生成的查询中看到的那样,sort参数将被忽略(为清楚起见进行了编辑):

select orderentit0_.id_order as id_order1_21_
...
from t_orders orderentit0_ 
cross join t_customers customeren1_ 
where 
    orderentit0_.id_customer=customeren1_.id_customer and 
    (orderentit0_.date_order between ? and ?) and 
    (? is null or lower(customeren1_.company_name) like concat('%'
lower(?)
'%') or lower(orderentit0_.order_code) like concat('%'
lower(?)
'%')) limit ?

我已经尝试简化WHERE表达式以删除LIKE条件,但是没有运气。

谢谢!

1 个答案:

答案 0 :(得分:1)

您的媒体资源的“导出”名称为date_order。不仅在json请求中,而且在请求参数中,都应使用这种方式。

@Column(name = "DATE_ORDER")
@JsonProperty("date_order")
private LocalDateTime orderDateTime;

尝试这种方式:

http://localhost:8080/api/v1/orders/search/findByOrderDateTimeBetweenAndSearchTerm?dateFrom=2018-01-01T00:00:00&dateTo=2018-12-01T00:00:00&sort=date_order&searchTerm=O-2018

(sort = date_order