演示
http://localhost:8080/swagger-ui.html
pom配置
//jetbrains://idea/navigate/reference?project=sprintboot&path=springboot-09-swagger/pom.xml:17:3
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
配置类
//jetbrains://idea/navigate/reference?project=sprintboot&path=com/cmk/springboot09swagger/config/SwaggerConfig.java:28:1
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket docket1(){
return new Docket(DocumentationType.SWAGGER_2).groupName("A");
}
@Bean
public Docket docket2(){
return new Docket(DocumentationType.SWAGGER_2).groupName("B");
}
@Bean
public Docket docket3(){
return new Docket(DocumentationType.SWAGGER_2).groupName("C");
}
// 配置了SwaggerDocketNbean实例
@Bean
public Docket docket(Environment environment) {
//设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev","test"); //只有dev 和 test环境才能使用
//通过environment.acceptsProfiles判断是否处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("默认")
.enable(flag) //enable是否启动Swagger,如果为False,则Swagger不能再浏范器中访间
.select()
//RequestHandlerSelectors 配置要扫描接口的方式
//basePackage:指定要扫描的包
//any() : 扫描全部
//none() : 不扫描
//withClassAnnotation: 扫描类上的注解
//withMethodAnnotation: 扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.cmk.springboot09swagger.controller"))
// .apis(RequestHandlerSelectors.withClassAnnotation(GetMapping.class))
// .apis(RequestHandlerSelectors.withMethodAnnotation(RestController.class))
//paths() 过滤什么路径
// .paths(PathSelectors.ant("/cmk/**"))
.build()
;
}
//作者信息
Contact contact = new Contact("cc", "https://www.baidu.com/", "xxx@qq.com");
//配置Swagger信息=apiInfo
private ApiInfo apiInfo() {
return new ApiInfo(
"c-swaggerApi文档",
"描述信息",
"v1.0",
"http://www.baidu.com",
contact,
"Apache 2.0",
"http://www.baidu.com",
new ArrayList<>()
);
}
}
控制器
//jetbrains://idea/navigate/reference?project=sprintboot&path=com/cmk/springboot09swagger/controller/HelloController.java:15:1
@RestController
public class HelloController {
@ApiOperation("Hello控制类")
@RequestMapping(value = "/hello")
public String hello(){
return "hello";
}
//只要我们的接口中,返回值中存在实体类,他就会被扫描到swagger中
@PostMapping(value = "/user")
public User user(){
return new User();
}
@ApiOperation("post测试类")
@PostMapping("/postt")
public User postt(@ApiParam("用户名") User user){
// int i = 5/0;
return user;
}
}
pojo
//jetbrains://idea/navigate/reference?project=sprintboot&path=com/cmk/springboot09swagger/pojo/User.java:11:2
@ApiModel("用户实体类")
public class User {
@ApiModelProperty("用户名")
public String username;
@ApiModelProperty("密码")
public String password;
}
资源类
//jetbrains://idea/navigate/reference?project=sprintboot&path=application.properties:1:1
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
spring.profiles.active=dev