This is the 7th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
preface
This section describes how to use Swagger in ruoyi-Vue’s Ruoyi-admin module. RuoYi uses Swagger3.0. An important part of our application is documentation. Swagger gives us a way to automatically generate documentation through code and annotations, which goes a long way toward ensuring the timeliness of API documentation.
Introduction of depend on
SpringBoot now supports Springfox-boot-starter, we can introduce Swagger3 with zero configuration.
<! -- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0. 0</version>
</dependency>
Copy the code
Of course higher Java versions (at least 8) and SpringBoot versions (after 2.2) are required
This time, as long as you add a dependency, you can directly use, very convenient, notice that this access address changed tohttp://localhost:8080/swagger-ui/index.html,However, the basic information of the current page has not been configured, so there is no way to display the information of the project. Swagger2 used to behttp://localhost:8080/swagger-ui.html
Configuring Basic Information
We can configure Swagger through application.yml, enable Swagger in dev and disable Swagger in production environment
swagger:
enabled: false
Copy the code
Note that the Spaces before false cannot be lost, and this is what it looks like when closed.
The code to add the configuration information is as follows. You can see that the @enableOpenAPI annotation can be omitted this time, so it really saves a lot of attention.
@Configuration
//@EnableOpenApi
public class SwaggerConfig {
/** * Enable swagger */
@Value("${swagger.enabled}")
private boolean enabled;
@Bean
public Docket docket(a) {
return new Docket(DocumentationType.OAS_30)
// Whether to enable Swagger
.enable(enabled)
// The basic information used to create the API is displayed in the document page (custom display information).
.apiInfo(apiInfo())
// Set which interfaces are exposed to Swagger
.select()
/ / scan all apis (RequestHandlerSelectors. Any ())
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(a) {
return new ApiInfoBuilder()
.title("XX Project Interface Document")
.description("XX Project Description")
.contact(new Contact("The author"."The author URL"."The author Email"))
.version("1.0") .build(); }}Copy the code