SpringBoot整合Swagger

SpringBoot整合Swagger

1.添加依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>

2.添加配置

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /** /api接口包扫描路径*/
    public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.example.group4";

    public static final String VERSION = "1.0.0";

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
                // 可以根据url路径设置哪些请求加入文档,忽略哪些请求
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //设置文档的标题
                .title("第4组Java作业接口文档")
                // 设置文档的描述
                .description(" 第4组作业 API 接口文档")
                // 设置文档的版本信息-> 1.0.0 Version information
                .version(VERSION)
                // 设置文档的License信息->1.3 License information
                .termsOfServiceUrl("http://www.baidu.com")
                .build();
    }
}

3.使用注解

@Api("User用户接口")
@RestController
public class UserController {
    /**
     * 获取单个用户信息
     * @return
     */
    @ApiOperation(value = "获取单个用户信息",notes = "获取单个用户信息")
    @GetMapping("/userInfo")
    public User getUserInfo()
    {
        User user = new User();
        user.setEmail("sean_xin@126.com");
        user.setUsername("Sean");
        user.setId(1);
        user.setPassword("123456");
        user.setSex("男");
        return user;
    }
}

4.测试应用

启动项目后,访问localhost:8080/swagger-ui.html

原文地址:https://www.cnblogs.com/seanRay/p/15126889.html