【问题标题】:Why is SpringFox not exposing @RepositoryRestResource?为什么 SpringFox 不公开@RepositoryRestResource?
【发布时间】:2020-05-09 02:40:39
【问题描述】:

我创建了一个@RepositoryRestResource

@RepositoryRestResource(collectionResourceRel = "tracks", path = "tracks")
public interface TrackRepository extends PagingAndSortingRepository<TrackEntity, Long> {
}

除了一些其他@RestController:

@RestController
@RequestMapping("/api")
public class UserController {

    private UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "/users", method = RequestMethod.POST)
    public @ResponseBody
    User postUser(@Validated @RequestBody Credentials credentials) {
        return this.userService.postUser(credentials); // Register user
    }

}

在我的 aplication.properties 我正在设置

spring.data.rest.base-path=/api

而这是@SpringBootApplication 入口点:

@SpringBootApplication
@EnableJpaRepositories(basePackages = {"io.app.spring.repository"})
@EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
@EntityScan(basePackages = "io.app.hibernate.model")
@EnableTransactionManagement
public class Application {

    private final static Logger LOGGER = LogManager.getLogger(Application.class);

    @PostConstruct
    void started() {
        TimeZone.setDefault(TimeZone.getTimeZone("Etc/UTC"));
    }

    @Autowired
    public Application(Environment environment) {
        LOGGER.info("");
        LOGGER.info("Active profiles:");
        for (String profile : environment.getActiveProfiles()) {
            LOGGER.info("  " + profile);
        }
        LOGGER.info("");
    }

    public static void main(String[] args) {
        LOGGER.debug("Running application ..");
        SpringApplication.run(Application.class, args);
    }

}

不过,我在https://localhost:8443/v3/api-docs 下看不到TrackRepository 的端点。只有来自UserController的人:

..
"/api/users": {
  "post": {
    "operationId": "postUser",
    "requestBody": {
      "content": {
        "*/*": {
          "schema": {
            "$ref": "#/components/schemas/Credentials"
          }
        }
      }
    },
    "responses": {
      "200": {
        "description": "default response",
        "content": {
          "*/*": {
            "schema": {
              "$ref": "#/components/schemas/User"
            }
          }
        }
      }
    }
  }
}
..

我正在使用 Spring Boot 2.2.2.RELEASE

这是我正在使用的整个 pom.xml

<?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>audio-platform</groupId>
    <artifactId>server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <spring.boot.version>2.2.2.RELEASE</spring.boot.version>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/>
    </parent>

    <dependencies>

        <!-- Spring Framework Boot -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

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

        <!-- Spring Security -->

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
            <version>1.0.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.3.2.RELEASE</version>
        </dependency>

        <!-- Spring Docs (Swagger) -->

        <!-- TODO After version upgrades check https://github.com/springdoc/springdoc-openapi/issues/133 -->

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-core</artifactId>
            <version>1.1.49</version>
            <exclusions>
                <exclusion>
                    <groupId>io.github.classgraph</groupId>
                    <artifactId>classgraph</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.1.49</version>
        </dependency>

        <dependency>
            <groupId>io.github.classgraph</groupId>
            <artifactId>classgraph</artifactId>
            <version>4.8.44</version>
        </dependency>

        <!-- Sentry -->

        <dependency>
            <groupId>io.sentry</groupId>
            <artifactId>sentry-spring</artifactId>
            <version>1.7.23</version>
        </dependency>

        <!-- PostgreSQL Driver -->

        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <version>42.1.4</version>
        </dependency>

        <!-- Flyway -->

        <dependency>
            <groupId>org.flywaydb</groupId>
            <artifactId>flyway-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.flywaydb.flyway-test-extensions</groupId>
            <artifactId>flyway-spring-test</artifactId>
            <version>5.0.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>

            <!-- Java version -->

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>

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

        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

    </build>

    <!-- Dependency Management -->

    <dependencyManagement>

        <dependencies>

            <!-- Import dependency management from Spring Boot -->

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

        </dependencies>

    </dependencyManagement>

</project>

我已经尝试按照here的建议添加springfox依赖项

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-data-rest</artifactId>
    <version>3.0.0-SNAPSHOT</version>
</dependency>

但它仍然无法正常工作。

任何想法可能是什么原因?

【问题讨论】:

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


    【解决方案1】:

    请尝试像这样将@Import(SpringDataRestConfiguration.class) 添加到您的应用程序配置中。

    compile('io.springfox:springfox-swagger2:2.7.0')
    compile('io.springfox:springfox-data-rest:2.7.0')
    compile('io.springfox:springfox-swagger-ui:2.7.0')
    
        @SpringBootApplication
        @EnableJpaRepositories(basePackages = {"io.app.spring.repository"})
        @EnableGlobalMethodSecurity(prePostEnabled = true, proxyTargetClass = true)
        @EntityScan(basePackages = "io.app.hibernate.model")
        @EnableTransactionManagement
        @Import(SpringDataRestConfiguration.class)//<-- Add thisconfiguration
        public class Application {
    

    示例:https://reflectoring.io/documenting-spring-data-rest-api-with-springfox/

    【讨论】:

    • 我的 Spring 版本中似乎没有这个类。还是我必须包含另一个依赖项?
    • 我为答案添加了更多上下文。希望有帮助
    • 这在 Spring Boot 1.5.4 上运行。我在 Spring Boot 2.2.2
    • springfox-data-rest 版本现在是 3.0.0,我遇到了同样的问题,v2/api-docs 有效,没有显示 swagger / index html
    猜你喜欢
    • 2011-01-04
    • 2014-11-06
    • 1970-01-01
    • 2019-05-07
    • 1970-01-01
    • 2020-01-08
    • 2019-02-27
    • 2014-08-29
    • 2011-10-25
    相关资源
    最近更新 更多