swagger接口测试

导入依赖:

<!-- 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/", "1516502843@qq.com");

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
posted @ 2021-10-14 22:05  吴大叔  阅读(539)  评论(0编辑  收藏  举报