zoukankan      html  css  js  c++  java
  • spring boot 整合 swagger2

    swagger2为了更好的管理api文档接口

    swagger构建的api文档如下,清晰,避免了手写api诸多痛点

    一,添加依赖

        <!--swagger2的官方依赖-->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.7.0</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.7.0</version>
            </dependency>                

     

    二,swagger2的配置类

    配置类需要与application.java至于同一目录下

    /**
     * @author canger
     * @Deseription Swagger2的配置类
     * 在与spring boot集成时,放在与Application.java同级的目录下。
     * 通过@Configuration注解,让Spring来加载该类配置。
     * 再通过@EnableSwagger2注解来启用Swagger2。
     * @create 2019/5/10 10:59
     **/
    @Configuration
    @EnableSwagger2
    public class Swagger2 {
    
        /**
         * 创建API应用
         * apiInfo() 增加API相关信息
         * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
         * 本例采用指定扫描的包路径来定义指定要建立API的目录。
         *
         * @return
         */
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.demo.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
        /**
         * 创建该API的基本信息(这些基本信息会展现在文档页面中)
         * 访问地址:http://项目实际地址/swagger-ui.html
         * @return
         */
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("Spring Boot中使用Swagger2构建RESTful APIs")
                    .description("服务发布 用户信息筛选")
                    .termsOfServiceUrl("http://localhost:8088/api")
                    .contact("卤煮蛋")
                    .version("1.0")
                    .build();
        }
    
    
    
    }

     

    三、设定访问API doc的路由

    在配置文件中,application.yml中声明:

    springfox.documentation.swagger.v2.path: /api-docs

    四,Controller层编写规范及注解功能

    常用注解

    @Api:用在类上,说明该类的作用。
    
    @ApiOperation:注解来给API增加方法说明。
    
    @ApiImplicitParams : 用在方法上包含一组参数说明。
    
    @ApiImplicitParam:用来注解来给方法入参增加说明。
    
    @ApiResponses:用于表示一组响应
    
    @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
    
          code:数字,例如400
    
          message:信息,例如"请求参数没填好"
    
          response:抛出异常的类   
    
    @ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)
    @ApiModelProperty:描述一个model的属性

    controller层

    @Controller
    @RequestMapping("/user")
    @Api(value = "/user",description = "用户信息的增删改查")
    public class UserController {
    
        @Autowired
        private IUserService userService;
    
    
      
        @RequestMapping(value = "/getPage",method = RequestMethod.GET ,produces = {"application/json" })
        @ResponseBody
        @ApiOperation(value = "获取用户列表",httpMethod = "GET",notes = "返回用户列表")
        @ApiResponses(
                @ApiResponse(code = 2001,message = "用户信息有误")
        )
        public ResultType getPage(
                @ApiParam(required = true,name = "current",value = "用户数据第几页")Integer current,
                @ApiParam(required = true,name = "size",value = "用户数据展示个数")Integer size
                ) throws BusinessException {
            Page<User> page = new Page<>(current,size);
            try {
                page = userService.selectPage(page);
            } catch (Exception e) {
                throw new BusinessException(ErrorCode.UNKNOW_ERROR);
    
            }
            List<User> users = page.getRecords();
            return ResultType.getResult(users);
        }
    }

    五,启动项目over

    启动SpringBoot项目,访问 http://localhost:自己端口/swagger-ui.html

  • 相关阅读:
    Oracle表级约束和列级约束
    什么是SSL证书服务?
    什么是阿里云SCDN
    什么是阿里云CDN
    什么是弹性公网IP?
    什么是云解析DNS?
    什么是DataV数据可视化
    什么是大数据计算服务MaxCompute
    什么是文件存储NAS
    什么是云存储网关
  • 原文地址:https://www.cnblogs.com/oldzhang1222/p/10856810.html
Copyright © 2011-2022 走看看