zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    POJ 2155 Matrix(二维树状数组)
    HDU 1280 前m大的数
    HDU 3183 A Magic Lamp(二维RMQ)
    HDU 3743 Frosh Week(归并排序求逆序数)
    POJ 2299 Ultra-QuickSort ( 归并排序 + 求逆序数 )
    HDU 1166 敌兵布阵(树状数组)
    HDU 2846 Repository(字典树)
    HDU 1896 Stones(优先队列)
    HDU 4393 Throw nails(优先队列)
    进程池
  • 原文地址:https://www.cnblogs.com/seanRay/p/15126889.html
Copyright © 2011-2022 走看看