Can Spring Data REST's QueryDSL integration be used to perform more complex queries?

后端 未结 3 1189
一向
一向 2020-11-30 22:49

I\'m currently building a REST API in which I want clients to easily filter on most properties of a specific entity. Using QueryDSL in combination with Spring Data REST (an

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 23:37

    As it was posted in some comment I also had the need to have different behaviour according to the field name creationDateFrom and creationDateTo. In order to make it work I did the following:

    First I added the @QueryEntity annotation and two more fields to my entity class. The fields were annotated with:

    • @Transient so the fields are not persisted
    • @Getter(value = AccessLevel.PRIVATE) as we are using Lombok, the annotation hides the field from the response body
    • @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) takes care of the format for parsing the date on the url query parameter

    @QueryEntity
    @Entity
    public class MyEntity implements Serializable {
      ...
    
      @Column(updatable = false)
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
      private Date creationDate;
    
      @Transient
      @Getter(value = AccessLevel.PRIVATE)
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
      private Date creationDateTo;
    
      @Transient
      @Getter(value = AccessLevel.PRIVATE)
      @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
      private Date creationDateFrom;
    
      ...
    }  
    

    Then I changed the way of generating the querydsl classes from JPAAnnotationProcessor to QuerydslAnnotationProcessor. This way fields annotated with @Transient are still generated on QMyEntity but are not persisted. Plugin configuration in pom:

    
        com.mysema.maven
        apt-maven-plugin
        1.1.3
        
            
                generate-sources
                
                    process
                
                
                    target/generated-sources/annotations
                    com.querydsl.apt.QuerydslAnnotationProcessor
                
            
        
    
    

    Finally I extended the QuerydslBinderCustomizer and customized the bindings related with the creationDateFrom and creationDateTo but applying the right logic over creationDate

    @Override
    default void customize(QuerydslBindings bindings, QMyEntity root) {
        bindings.bind(root.creationDateFrom).first((path, value) -> 
                                                    root.creationDate.after(value));
        bindings.bind(root.creationDateTo).first((path, value) ->
                                                   root.creationDate.before(value));
    }
    

    With all of this you can do date range queries using one, both or none of the criterias:

    http://localhost:8080/myentities?creation_date_to=2017-05-08
    http://localhost:8080/myentities?creation_date_from=2017-01-01
    http://localhost:8080/myentities?creation_date_from=2017-01-01&creation_date_to=2017-05-08
    

提交回复
热议问题