1. Overview
1.概述
In this tutorial, we’ll learn multiple methods to configure Swagger in a Spring Boot application to hide paths exposed by the BasicErrorController.
在本教程中,我们将学习多种方法来配置Swagger中的Spring Boot应用程序以隐藏由BasicErrorController暴露的路径。
2. Target Project
2.目标项目
We will not cover creating the basic configuration to start with Spring Boot and Swagger-UI in this article. We can either use an already-configured project or follow the Setting Up Swagger 2 with a Spring REST API guide to create the basic configuration.
在本文中,我们将不涉及创建基本配置以开始使用 Spring Boot 和 Swagger-UI。我们可以使用一个已经配置好的项目,或者按照Setting Up Swagger 2 with a Spring REST API指南来创建基本配置。
3. The Problem
3.问题
If our code contains a BasicErrorController, Swagger, by default, will include all its endpoints also in the generated documentation. We need to provide a custom configuration to remove unwanted controllers.
如果我们的代码包含一个BasicErrorController,Swagger默认会将其所有的端点也包含在生成的文档中。我们需要提供一个自定义配置来移除不需要的控制器。
For example, let’s say that we would like to provide the API documentation of a standard RestController:
例如,假设我们想提供一个标准RestController的API文档。
@RestController
@RequestMapping("good-path")
public class RegularRestController {
@ApiOperation(value = "This method is used to get the author name.")
@GetMapping("/getAuthor")
public String getAuthor() {
return "Name Surname";
}
// Other similar methods
}
Also, let’s suppose our code includes a class that extends the BasicErrorController:
另外,我们假设我们的代码包括一个扩展了BasicErrorController的类。
@Component
@RequestMapping("my-error-controller")
public class MyErrorController extends BasicErrorController {
// basic constructor
}
We can see that my-error-controller is included in the generated documentation:
我们可以看到,my-error-controller包含在生成的文档中。
4. The Solutions
4.解决方案
In this section, we’ll look at four different solutions to exclude resources from the Swagger documentation.
在这一节中,我们将看看四种不同的解决方案,将资源从Swagger文档中排除。
4.1. Exclude with basePackage()
4.1.用basePackage()排除
By specifying the base package of the controllers we want to document, we can exclude resources that we don’t need.
通过指定我们想要记录的控制器的基础包,我们可以排除我们不需要的资源。。
This works only when the error controller package is different from the standard controller’s package. With Spring Boot, it’s enough if we provide a Docket bean:
这只有在错误控制器的包与标准控制器的包不同的情况下才有效。在Spring Boot中,如果我们提供一个Docketbean就足够了。
@Configuration
public class SwaggerConfiguration {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.baeldung.swaggerconf.controller"))
.build();
}
}
With this custom configuration in place, Swagger will check for REST Controller methods only inside the specified package. So, for example, if our BasicErrorController is defined in the package “com.baeldung.swaggerconf.error“, it will not be considered.
有了这个自定义配置,Swagger将只在指定的包内检查REST控制器方法。因此,例如,如果我们的BasicErrorController被定义在包”com.baeldung.swaggerconf.error“中,它将不会被考虑。
4.2. Exclude with Annotations
4.2.带注解的排除法
Alternatively, we could also indicate that Swagger has to generate documentation only of classes decorated with a specific Java annotation.
另外,我们也可以指出,Swagger必须只生成用特定Java注解装饰的类的文档。
In this example, we’ll set it to RestController.class:
在这个例子中,我们将把它设置为RestController.class:。
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.build();
}
In this case, the BasicErrorController will be excluded from Swagger documentation because it’s not decorated with the @RestController annotation. This annotation is instead present on the RegularRestController we would like to document.
在这种情况下,BasicErrorController将被排除在Swagger文档之外,因为它没有用@RestController注解装饰。而这个注解存在于我们想要记录的RegularRestController上。
4.3. Exclude with Regex on Path
4.3.使用路径上的Regex排除法
Another approach is to specify a regex on a custom path. In this case, only resources mapped to the “/good-path” prefix will be documented:
另一种方法是在自定义路径上指定一个重码。在这种情况下,只有映射到”/go-path”前缀的资源将被记录。
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
.paths(regex("/good-path/.*"))
.build();
}
4.4. Exclude with @ApiIgnore
4.4.用@ApiIgnore排除
Lastly, we can exclude a specific class from Swagger by using the annotation @ApiIgnore:
最后,我们可以通过使用注解 @ApiIgnore:从Swagger中排除一个特定的类。
@Component
@RequestMapping("my-error-controller")
@ApiIgnore
public class MyErrorController extends BasicErrorController {
// basic constructor
}
5. Conclusion
5.总结
In this article, we presented four different ways to configure Swagger in a Spring Boot application to hide BasicErrorController resources.
在这篇文章中,我们介绍了在Spring Boot应用程序中配置Swagger以隐藏BasicErrorController资源的四种不同方式。
As always, the complete code is available over on GitHub.
一如既往,完整的代码可在GitHub上获得,。