Srping Boot整合Swagger2

104 阅读1分钟

1. 引入依赖

<!-- swagger2 配置 -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.4.0</version>
</dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.4.0</version>
</dependency>
<!-- Swagger的第三方UI,更美观 -->
<dependency>
   <groupId>com.github.xiaoymin</groupId>
   <artifactId>swagger-bootstrap-ui</artifactId>
   <version>1.6</version>
</dependency>

2. 编写配置类

package com.imooc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {

// http://localhost:8088/swagger-ui.html     官方路径
// http://localhost:8088/doc.html 第三方UI路径

    // 配置swagger2核心配置 docket
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2) // 指定api类型为swagger2
                    .apiInfo(apiInfo()) // 用于定义api文档汇总信息
                    .select() // 返回ApiSelectorBuilder,对应最后的.build()
                    .apis(RequestHandlerSelectors
                            .basePackage("com.imooc.controller")) // 指定controller包
                    .paths(PathSelectors.any()) // 选择controller包下的所有controller
                    .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("天天吃货 电商平台接口api")        // 文档页标题
                .contact(new Contact("imooc",
                        "https://www.imooc.com",
                        "abc@imooc.com"))        // 联系人信息
                .description("专为天天吃货提供的api文档")  // 详细信息
                .version("1.0.1")   // 文档版本号
                .termsOfServiceUrl("https://www.imooc.com") // 网站地址
                .build();
    }

}

3. 优化显示

3.1 不显示某些端口

@ApiIgnore
@RestController
public class StuFooController { ... }

3.2 显示接口中文名称

@Api(tags = {"注册登录"})
@RestController
@RequestMapping("passport")
public class PassportController { ... }

3.3 给接口内的方法添加中文信息

@ApiOperation(value = "用户名是否存在value", notes = "用户名是否存在notes", httpMethod = "GET")
@GetMapping("/usernameIsExist")
public IMOOCJSONResult usernameIsExist(@RequestParam String username) { ... }

3.4 给入参添加信息

@ApiModel(value = "用户对象BO value", description = "从客户端,由用户传入的数据封装在此entity中 description")
public class UserBO {
    @ApiModelProperty(value = "用户名value", name = "username name", example = "imooc example", required = true)
    private String username;
    @ApiModelProperty(value = "密码value", name = "password name", example = "123456 example", required = true)
    private String password;
    @ApiModelProperty(value = "确认密码value", name = "confirmPassword name", example = "123456 example", required = false)
    private String confirmPassword;
}

3.5 给出参添加信息

@ApiOperation(value = "渠化配置/灯组设置、车道配置列表", notes = "渠化配置/灯组设置、车道配置列表", response = JsonViewObject.class,
        produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
@PostMapping(value = "/listLaneInfo",
        produces = MediaType.APPLICATION_JSON, consumes = MediaType.APPLICATION_JSON)
@ApiResponses({
        @ApiResponse(code = 200, message = "OK", response = SaveLaneInfoDTO.class),
})
public JsonViewObject listLaneInfo(@RequestBody CrossIdDTO crossIdDTO) { ... }