本文介绍了Swagger 未使用 Spring Boot 检测 Spring Data Rest API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个 Spring Boot 项目,包括 Spring Data Rest 和 Swagger:

I setup a Spring Boot project including Spring Data Rest and Swagger:

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.4.0</version>
</dependency>
<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.4.0</version>
</dependency>

这是我的 Swagger 配置:

This is my Swagger configuration:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

摘自 application.properties:

Excerpt from application.properties:

spring.data.rest.base-path=/api
server.context-path=/myapp

当我运行服务器时,所有的 rest 端点都被正确映射并可以访问到 /myapp/api/...,包括我自己创建的自定义 RestController.

When I run the server, all rest endpoints are correctly mapped and reachable to /myapp/api/..., including a custom RestController I created on my own.

但是:

  • http://localhost:8080/myapp/api可以看到Spring的列表Data Rest API(Json 格式),但看不到我的自定义 RestController 端点.

  • at http://localhost:8080/myapp/api I can see the list of SpringData Rest APIs (in Json format) but cannot see my custom RestController endpoints.

http://localhost:8080/myapp/swagger-ui.html 我看到一个不错的 gui,它只列出了我的自定义 RestController 和错误端点,而不是 Spring Data Rest蜜蜂.事实上, http://localhost:8080/myapp/v2/api-docs 确实不引用 Spring Data Rest 端点,而只引用我的自定义 RestController 和错误端点.

at http://localhost:8080/myapp/swagger-ui.html I see a nice gui which only lists my custom RestController and the error endpoint, not the Spring Data Rest APIs. In fact, http://localhost:8080/myapp/v2/api-docs does not make any reference to Spring Data Rest endpoints, but only to my custom RestController and to the error endpoint.

如何修复我的 Spring Data Rest &Swagger 配置?

How can I fix my Spring Data Rest & Swagger configuration?

推荐答案

Spring Data Rest 支持 仅在 springfox 2.6.0 版本中引入.如果您在升级到之后按照说明最新版本的 springfox(撰写本文时为 2.6.1)您应该不会在渲染端点时遇到问题.

Spring Data Rest support was only introduced in springfox version 2.6.0. If you follow the instructions after upgrading to the latest version of springfox (2.6.1 at the time of this writing) you shouldn't have a problem with rendering the endpoints.

这篇关于Swagger 未使用 Spring Boot 检测 Spring Data Rest API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:49