Préface
swagger2, en tant qu'outil puissant pour la génération d'interfaces API, facilite grandement le coût du débogage front-end et back-end. Les développeurs back-end n'ont pas besoin de prêter attention au peignage des documents d'interface et peuvent être présentés de manière conviviale dans des mots d'interface.
Combat réel
- Importez le fichier pom.xml dans le package jar:
<!-- 集成Swagger2接口测试工具(类似于postman) -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
- Pour démarrer la classe principale de springboot, ajoutez les annotations suivantes:
//开启对swagger的支持
@EnableSwagger2
- Créez une nouvelle classe de configuration pour swagger2:
package com.plugs.swagger;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
public class Swagger2Config {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档")
.description("")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
-
Visitez: ip: port / nom du projet / swagger-ui.html
-
Obtenez l'adresse complète du code source: https://download.csdn.net/download/penggerhe/11670196
-
Suivez le compte officiel et recevez-le gratuitement: