一、swagger2官網(wǎng)
swagger2 官網(wǎng):https://swagger.io/
二、maven 地址
springfox-swagger2:http://mvnrepository.com/artifact/io.springfox/springfox-swagger2
springfox-swagger-ui:http://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui
三嫌吠、在 pom.xml 添加依賴
<!--swaggerApi-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
四止潘、創(chuàng)建com.zhg.swagger.SwaggerConfig配置類
package com.zhg.demo.swagger;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;
@Configuration
@ConditionalOnProperty(prefix = "swagger",value = {"enable"},havingValue = "true")//enable
@EnableWebMvc
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zhg.demo.controller"))
.paths(PathSelectors.any())
.build();
}
@SuppressWarnings("deprecation")
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("測(cè)試swagger")
.description("RESTful APIs 接口文檔集合")
.termsOfServiceUrl("http://www.lemos.club/")
.contact("zhg")//作者
.version("1.0")
//.license("The Apache License, Version 2.0")
.build();
}
}
五、在springboot啟動(dòng)類中添加注解@EnableSwagger2
package com.zhg.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class SbApplication {
public static void main(String[] args) {
SpringApplication.run(SbApplication.class, args);
}
}
六辫诅、測(cè)試
-輸入http://localhost:9091/swagger-ui.html#/測(cè)試
七凭戴、注意
-在pom中不能缺少spring-boot-starter-web核心依賴
<!--使用Spring MVC構(gòu)建Web(包括RESTful)應(yīng)用程序的入門者。使用Tomcat作為默認(rèn)嵌入式容器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>