swagger介面測試
阿新 • • 發佈:2021-10-14
匯入依賴:
<!-- swagger start -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger end-->
傳建一個config類:
import com.google.common.base.Optional;
import com.google.common.collect.Lists;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.DefaultGenericTypeNamingStrategy;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.ApiSelector;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2 //開啟Swagger2
public class SwaggerConfig {
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("WZJ");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("HHH");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("Hello");
}
//配置Swagger的Docket的Bean例項
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("JJ")
//enable():是否啟用Swagger,若是False:swagger在瀏覽器中不可訪問
.enable(true)
.select()
//RequestHandlerSelectors,配置要掃描介面的方式
//basePackage:指定要掃描的包
//any():掃描全部
//none():不掃描
//withClassAnnotation()掃描類上的註解
//withMethodAnnotation()掃描方法上的註解
.apis(RequestHandlerSelectors.basePackage("com.jj.springshop.controller"))
//過濾什麼路徑
//.paths(PathSelectors.ant("/jj/**"))
.build()
;
}
//配置Swagger資訊=apiInfo
private ApiInfo apiInfo() {
Contact contact = new Contact("WZJ", "https://www.cnblogs.com/WuDaShu/", "[email protected]");
return new ApiInfo(
"WZJ-Swagger",
"Its nothing",
"1.0",
"urn:tos",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList()
);
}
}
啟動專案後瀏覽網址:
http://localhost:8080/swagger-ui.html