Hide/remove Spring Swagger2 中的 MVC 端点

Hide/remove Spring MVC endpoint in Swagger2

我正在为 API UI 使用 Swagger 2。所以,我的 gradle.build 有:

compile "io.springfox:springfox-swagger2:${swaggerVersion}"
compile "io.springfox:springfox-swagger-ui:${swaggerVersion}"

我已将 Swagger 配置如下:

@Configuration
@Profile("!production")
@EnableSwagger2
@ComponentScan(basePackageClasses = com.company.controllers.ContentController.class)
public class SwaggerConfiguration {
    @Autowired
    private BuildInfo buildInfo;

    @Bean
    public Docket awesomeApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.awesomeApiInfo())
                .select()
                .apis(Predicates.not(RequestHandlerSelectors.basePackage("org.springframework.boot")))
                .build();

    }

    private ApiInfo awesomeApiInfo() {
        return new ApiInfoBuilder()
                .title("Awesome API - build #" + this.buildInfo.getVersion())
                .description("Enter the IDs in order to look for the content")
                .version("0.1")
                .build();
    }
}

我得到了我定义的 api 端点,但也得到了 Spring MVC 端点,如下所示:

现在,我需要摆脱这些 mvc 端点。

非常感谢任何帮助!!

您可以遵循的最佳方法是限制对 ServiceStack 的可见性和访问。因此,您可以通过以下方式隐藏它,使其在外部不可见:

[Restrict(VisibleInternalOnly = true)]
public class InternalAdmin { }

您可以阅读更多相关信息here

哦...实际上这是我愚蠢的错误。我将 RequestHandlerSelectors 更改为 select 只有来自我自己的控制器包的端点,如下所示:

 @Bean
    public Docket awesomeApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.awesomeApiInfo())
                .select()
                .paths(PathSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.company.awesome.controllers"))
                .build();

    }

这仅显示 controller 包中 类 内映射的端点。

另一种指定基础包的方法是创建一个 class 注释,如下所示:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface SwaggerDocumentation {
}

然后根据需要在控制器上使用它:

@RestController
@SwaggerDocumentation
public class EntityRestController {

    EntityService entityService;


    @Autowired
    public EntityRestController(final EntityService entityService) {
        this.entityService = entityService;
    }

    @GetMapping("/status")
    String getTest() {
        return "Ready";
    }

    @GetMapping("/api/entities")
    Collection<Entity> getEntities() {
        return entityService.findSome();
    }

}

然后最后在 SwaggerConfig class

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