Swagger

入门

  1. 创建springboot-web项目

  2. 导入依赖

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
      <!-- 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>
  3. 编写配置类

    1
    2
    3
    4
    5
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {

    }
  4. 测试

    编写接口访问/swagger-ui.html路径

配置信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.mui.study.controller"))
.build()
;
}

private ApiInfo apiInfo(){
Contact DEFAULT_CONTACT = new Contact("作者", "https://muieay.top", "qq@qq.com");
return new ApiInfo(
"标题",
"描述信息",
"1.0版本号",
"urn:https://muieay.top",
DEFAULT_CONTACT,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}