Почему SpringFox не выставляет @RepositoryRestResource? - PullRequest
2 голосов
/ 22 января 2020

Я создал @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);
    }

}

Тем не менее, я не вижу конечную точку (точки) для TrackRepository в https://localhost: 8443 / v3 / API-документы . Только те из 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>

Я уже пытался добавить 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 Ответ

0 голосов
/ 23 января 2020

попробуйте добавить @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/

...