Java 使用Spring数据Rest进行Spring引导动态查询

Java 使用Spring数据Rest进行Spring引导动态查询,java,spring,spring-boot,spring-data,spring-data-rest,Java,Spring,Spring Boot,Spring Data,Spring Data Rest,我正在尝试做一些有趣的事情,希望能够使用SpringBoot1.5.9和SpringDataREST动态构建sql查询过滤器,而无需编写控制器。我觉得我可能在正确的道路上,但我有点被卡住了 其思想是使用HandlerInterceptorAdapter拦截HTTP请求GET方法,并将请求查询参数存储到一个对象中,该对象可由PagingAndSortingRepository使用。计划是覆盖SpringDataREST默认使用的findAll(可分页的p),调用findAll(规范s,可分页的p)

我正在尝试做一些有趣的事情,希望能够使用SpringBoot1.5.9和SpringDataREST动态构建sql查询过滤器,而无需编写控制器。我觉得我可能在正确的道路上,但我有点被卡住了

其思想是使用HandlerInterceptorAdapter拦截HTTP请求GET方法,并将请求查询参数存储到一个对象中,该对象可由PagingAndSortingRepository使用。计划是覆盖SpringDataREST默认使用的findAll(可分页的p),调用findAll(规范s,可分页的p)以允许动态过滤

我使用了Spring提供的请求范围来保存查询参数,以便在请求的整个生命周期中使用它们

问题是我无法从存储库接口文件访问QueryParam对象。我尝试通过遵循此stackoverflow解决方案,将依赖项注入接口文件,但不起作用:

SpringDataREST尝试通过基于函数名构建SQL查询来创建端点。即使我试图通过创建一个可以查询的函数来欺骗Spring数据,然后用我在下面实现的SampleRepositoryComponents覆盖它来绕过这个问题,我也无法获得存储在QueryParam中的数据

其他人也发出了拉取请求,允许Spring数据Rest忽略某些方法:

查询参数:

public class QueryParam {
  String query_string;
  Map<String, String[]> parameter_map;

  public String getQuery_string() {
    return query_string;
  }
  public void setQuery_string(String query_string) {
    this.query_string = query_string;
  }
  public Map<String, String[]> getParameter_map() {
    return parameter_map;
  }
  public void setParameter_map(Map<String, String[]> parameter_map) {
    this.parameter_map = parameter_map;
  }
}

在SpringDataREST和SpringDataExtensions的帮助下,同样的功能已经在SpringDataREST中实现了

只需从
querydsldpredicateexecutor
扩展您的repo(并且可以选择
QuerydslBinderCustomizer
,默认实现
customize
方法,如示例所示),然后您就可以通过过滤查询您的repo数据(更多其他-具有分页和排序支持):

不要忘记将QueryDSL依赖项添加到项目中(例如,如下所示):


com.querydsl
querydsl jpa
com.querydsl
querydsl公寓
假如
com.mysema.maven
aptmaven插件
1.1.3
过程
目标/生成的源/注释
com.querydsl.apt.jpa.JPAAnnotationProcessor

然后编译您的项目(例如
mvncile
),让它生成“Q”类。

这正是我想要的。我试图实现类似于Loopback的功能,而这个功能基本上就是这样。我不敢相信我没有在文档中认出这一点。我并不清楚。
@RepositoryRestResource(collectionResourceRel = "Sample", path = "Sample")
public interface SampleRepository extends PagingAndSortingRepository<Sample, UUID>,
    JpaSpecificationExecutor<Sample> {
  // Cannot just use any custom name
  @RestResource(exported = false)
  public QueryParam getQuery_param();

  default public Page<Sample> findAll(Pageable p) {
    QuerySpecification<Sample> qs = new QuerySpecification<Sample>(getQuery_param());

    return findAll(qs, p);
  }
}
public class QuerySpecification<T> implements Specification<T> {
  private QueryParam query_param;

  public QuerySpecification(QueryParam query_param) {
    this.query_param = query_param;
  };

  @Override
  public Predicate toPredicate(Root<T> rt, CriteriaQuery<?> cq, CriteriaBuilder cb) {
    // Build your custom predicate here
    return null;
  }
}
@Component
public abstract class SampleRepositoryComponents implements SampleRepository {
  @Autowired
  private QueryParam query_param;

  @Override
  public QueryParam getQuery_param() {
    return this.query_param;
  }

  public void setQuery_param(QueryParam query_param) {
    this.query_param = query_param;
  }
}
/myEntities?field1=value1&field2=value2&sort=field2,desc&size=10&page=2
<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-jpa</artifactId>
</dependency>

<dependency>
    <groupId>com.querydsl</groupId>
    <artifactId>querydsl-apt</artifactId>
    <scope>provided</scope>
</dependency>

<build>
    <plugins>
        <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.1.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-sources/annotations</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>                                                       
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>