本文介绍了使用 spring-boot 内部 @Controllers 和 @RequestParam 提供搜索方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个带有弹簧靴的后端休息 api.

I am building a back-end rest api with spring boot.

实体:

@Entity
public class Club {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @NotNull
    @Column(unique=true)
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}

存储库:

@RepositoryRestResource
public interface ClubRepository extends JpaRepository<Club, Long>, JpaSpecificationExecutor<Club> {

}

仅此一项就在 http://host/clubs 暴露了一个休息端点,太好了.现在,我想在 url 中允许一些参数用于搜索目的,所以我开始按照 http://www.baeldung.com/rest-api-search-language-spring-data-specifications.

This alone exposes a rest endpoint at http://host/clubs, great. Now, I'd like to allow some parameters in the url for search purposes, so I started following the instructions of http://www.baeldung.com/rest-api-search-language-spring-data-specifications.

但他们最终创建了一个自定义的 @Controller 来传递请求参数:

But they end-up creating a custom @Controller to pass the request params:

@Controller
public class ClubController {

    @Autowired
    private ClubRepository repo;

    @RequestMapping(method = RequestMethod.GET, value = "/clubs")
    @ResponseBody
    public List<Club> search(@RequestParam(value = "search") String search) {
        /* ... */
        return repo.findAll(spec);
    }
}

所以你看,他们最终调用了存储库的 findAll 方法,只是传递了他们基于查询参数构建的规范.很简单,但我真的很想不必为我的每个域对象创建额外的控制器.换句话说,有没有办法通过直接注释(例如)@Entity 或覆盖存储库中的方法(如 findAll 方法)来提供此搜索功能?

So you see, they end up calling the findAll method of the repository, just passing a specification they build based on the query parameter. Easy enough, but really I'd love to not have to create additional controllers for each of my domain objects. In other words, is there a way to provide this search feature by directly annotating (for example) the @Entity, or overriding methods (like the findAll method) in the repository?

推荐答案

更好的方法是使用 QueryDsl 而不是标准 API.除了一些基本配置之外,Spring Data 以及 Spring Data Rest 将免费"为您提供许多出色的功能.

A better approach is to use QueryDsl rather than the criteria API. Spring Data and, by extension, Spring Data Rest will then give you a lot of excellent functionality 'for free' other than some basic configuration.

https://spring.io/blog/2015/09/04/what-s-new-in-spring-data-release-gosling#spring-data-rest

https://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

一个配置你的存储库界面就变成了:

One configured your repository interface simply becomes:

@RepositoryRestResource
public interface ClubRepository extends JpaRepository<Club, Long>, QueryDslPredicateExecutor<Club> {

}

然后可以使用参数的动态组合来查询端点:

An endpoint can then be queried using a dynamic combination of parameters:

例如

http://host/clubs?name=someName

http://host/clubs?name=someName&otherProperty=X&sort=name,desc

不需要进一步的代码,即没有自定义控制器、没有规范和查询方法.

要配置 Maven,只需将以下内容添加到包含您的实体的模块中的 POM:这将生成必要的查询"类.

For configuring Maven simply add the following to the POM in the module containing your entities: this will generate the necessary 'query' classes.

<dependencies>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>${querydsl.version}</version>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>${querydsl.version}</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

<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/java</outputDirectory>
                        <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

这篇关于使用 spring-boot 内部 @Controllers 和 @RequestParam 提供搜索方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:59