当前位置: 首页 > 知识库问答 >
问题:

为什么SpringFox没有公开@RepositoryRestResource?

韦俊英
2023-03-14

我已经创建了一个@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

而这是@springbootplication的入口点:

@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);
    }

}

仍然,我没有看到TrackRepository下的endpointhttps://localhost:8443/v3/api-docs.只有那些来自UserController的endpoint:

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

我使用的是Spring Boot2.2.2。释放

这是我使用的整个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>

我已经尝试按照这里的建议添加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>

但它仍然不起作用。

知道这是什么原因吗?

共有1个答案

卜盛
2023-03-14

请尝试添加@导入(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/

 类似资料:
  • 这是一个取自现有代码库的cucumber场景大纲,但我被要求使用TestNG尝试它。一些东西(场景(),场景(),TestNGCucumberRunner,数据提供商)已经从stackoverflow上的另一个帖子中删除,因为我不知道如何使用TestNG使场景大纲发挥良好。 在执行时,唯一的错误是NullPointerException,因为WebDriver获取其值时@BeforeClass未触

  • 问题内容: 我正在尝试做这样的事情: 不幸的是,即使在Java 9中也不存在。 为什么它被遗漏了? 建议的解决方法是什么? 问题答案: 为什么它被遗漏了? 该API提供了可重用的构建块。这里的相关积木是,,。通过这些,您可以实现所需的功能:将流内映射到对象,然后获得平面图。提供构建基块的排列是不切实际的,并且很难扩展。 建议的解决方法是什么? 如前所述,使用可用的构建基块(+ ):

  • 许多编译器都提供128位整数类型,但我使用过的编译器都没有提供typedefs。为什么? 据我回忆,标准 用于此目的的储量 鼓励提供此类类型的实现提供typedef 要求此类实现提供至少128位的intmax_t (而且,我不相信我使用了实际上符合最后一点的实现)

  • 我也下载了JDK,我在每个论坛上看到这个问题,都说要添加变量JDK\u HOME或JAVA\u HOME,我两个都试过了,但仍然没有打开。 我重新安装了Android Studio并重新启动了我的计算机,但它没有打开。我得到了这样的加载图标,但它只是卡在上面。 我正在运行Windows 8.1,并且安装了最新版本的JDK。非常高兴能开始编写代码,如有任何帮助或建议,将不胜感激。

  • 我正在与Kotlin研究地图,并决定运行以下代码: 这让我: 我以为的冲突导致了一个列表。然后当我尝试这是静态编程语言的时,我也没有得到任何冲突。 问题: 我在这个简单的碰撞示例中错过了什么? 每个实现最常见的冲突行为是什么?

  • 这个问题解决了。请看下面的核对答案。 注入点有以下注释:-@org.springframework.beans.factory.annotation.autowired(required=true) 行动: 考虑在您的配置中定义一个类型为'DAO.UserRepository'的bean。 进程结束,退出代码为%1