SpringFox,Spring Data Rest与JpaRepository - Swagger无法识别使用Spring Boot的Rest存储库。

6

我在使用Springfox Swagger 2进行API文档编写时遇到了麻烦。它无法识别我的rest repositories(JpaRepository/PagingAndSortingRepository)。

import org.springframework.data.jpa.repository.JpaRepository;

import com.aegon.dop.model.Book;

public interface BookRepository extends JpaRepository<Book, Long> {
}

我尝试添加Swagger注释,像下面这样,但没有成功

@Api(tags = "books")
@RepositoryRestResource(collectionResourceRel = "books", path = "books")
public interface BookRepository extends JpaRepository<Book, String> {
    @ApiOperation("Find all books by book name")
    Dopadviseur findByBookName(@Param("bookName") @RequestParam @ApiParam(value = "bookName") String bookName);
}

然而,HAL浏览器可以识别我的rest仓库。有人能帮我解决这个问题吗?以下是我的pom依赖项:

  <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.company</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>Demo</name>
    <description>Demo application</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-boot-admin.version>2.0.1</spring-boot-admin.version>
        <spring-fox-version>2.9.2</spring-fox-version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>       
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0</version>
        </dependency>

        <!-- Swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${spring-fox-version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-data-rest</artifactId>
            <version>${spring-fox-version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${spring-fox-version}</version>
        </dependency>

        <!-- Soap Webservices -->
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
        </dependency>

    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>de.codecentric</groupId>
                <artifactId>spring-boot-admin-dependencies</artifactId>
                <version>${spring-boot-admin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>           
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>           
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>central</id>
            <name>Central Repository</name>
            <url>http://repo.maven.apache.org/maven2</url>
        </repository>       
    </repositories>
</project>

这是我的启动类,

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;

import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@Import({ SpringDataRestConfiguration.class })
public class DopApplication {
    public static void main(String[] args) {
        SpringApplication.run(DopApplication.class, args);
    }
}

当我添加 @Import({ SpringDataRestConfiguration.class }) 后,应用程序启动时会出现以下错误。
   Caused by: java.lang.NoSuchMethodError: org.springframework.data.repository.support.Repositories.getRepositoryInformationFor(Ljava/lang/Class;)Lorg/springframework/data/repository/core/RepositoryInformation;
    at springfox.documentation.spring.data.rest.EntityServicesProvider.requestHandlers(EntityServicesProvider.java:81) ~[springfox-data-rest-2.9.2.jar:null]
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138) ~[springfox-spring-web-2.9.2.jar:null]
    at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135) ~[springfox-spring-web-2.9.2.jar:null]
    at com.google.common.collect.Iterators$7.transform(Iterators.java:750) ~[guava-20.0.jar:na]

你找到这个问题的解决方案了吗?我怀疑需要 SpringFox 的 3.0.0 版本,但不幸的是它还没有发布。 - mxb
不好意思,我还没有找到任何解决方案。 - Nagarjuna Adapa
1个回答

1

介绍

我认为你需要在你的代码中包含以下内容:

@Import({springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration.class})

请查看以下讨论您问题的主题:

https://github.com/springfox/springfox/issues/699#issuecomment-270052170

Github上的完整建议答案:

@SpringBootApplication @EnableSwagger2 @Import({springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration.class}) public class ServletInitializer extends SpringBootServletInitializer {

更新

我认为你应该升级到:

Spring Fox 2.9.0 : https://github.com/springfox/springfox/issues/2372 他们提到了一个与您非常相似的问题,该版本已解决此问题。

https://github.com/springfox/springfox/issues/2272


嗨,Menelaos,感谢您的回复。 我也尝试了这个解决方案。 现在我在应用程序启动时遇到了这个错误。ConfigServletWebServerApplicationContext:上下文初始化期间发生异常-取消刷新尝试:org.springframework.context.ApplicationContextException:无法启动bean 'documentationPluginsBootstrapper';嵌套异常是java.lang.NoSuchMethodError:org.springframework.data.repository.support.Repositories.getRepositoryInformationFor(Ljava / lang / Class;) Lorg / springframework / data / repository / core / RepositoryInformation; - Nagarjuna Adapa
@Nagarjuna Adapa 请尝试升级到 springfox 的 2.9.0 版本。在您的 pom.xml 中更改定义。 - Menelaos
1
我正在使用 springfox 2.9.2,即使如此,我仍然遇到了相同的问题。我已经编辑了我的问题,并附上了 pom 和主类。 - Nagarjuna Adapa
@Nagarjuna Adapa,可以提供带有所有依赖项的整个pom.xml文件吗?我将尝试创建一个项目并对其进行测试。 - Menelaos
有人解决了这个问题吗?我和这里一样遇到了完全相同的问题/配置,但似乎无法让Swagger UI(最新版本3.0.0-SNAPSHOT)显示由Spring Data REST生成的端点。 - Orby

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接