zoukankan      html  css  js  c++  java
  • Springboot:Springboot+Swagger2集成服务

    Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。

    Pom依赖

    在pom.xml文件中添加swagger相关依赖

    <!-- Swagger依赖包 -->
    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
    </dependency>
    <!-- Swagger的ui界面 -->
    <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
    </dependency>

    配置Swagger 

    新建Swagger配置类,需要特别注意的是Swagger scan base package,这是扫描注解的配置,即你的API接口位置,对前端提供服务接口的位置。

    package com.example.demo.config;
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder().title("API接口文档")
                    .description("用户信息管理")
                    .version("1.0.0")
                    .build();
        }
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) //这里写的是API接口所在的包位置
     
                    .paths(PathSelectors.any())
                    .build();
        }
    }

    Swagger常用注解

    - @Api()用于类; 

      表示标识这个类是swagger的资源 
    - @ApiOperation()用于方法; 

      表示一个http请求的操作 
    - @ApiParam()用于方法,参数,字段说明; 

      表示对参数的添加元数据(说明或是否必填等) 
    - @ApiModel()用于类 

      表示对类进行说明,用于参数用实体类接收 
    - @ApiModelProperty()用于方法,字段 

      表示对model属性的说明或者数据操作更改 
    - @ApiIgnore()用于类,方法,方法参数 

      表示这个方法或者类被忽略 
    - @ApiImplicitParam() 用于方法 

      表示单独的请求参数 
    - @ApiImplicitParams() 用于方法,

      包含多个 @ApiImplicitParam

     使用举例说明

     @Api() 
    用于类;表示标识这个类是swagger的资源 
    tags–表示说明 
    value–也是说明,可以使用tags替代 
    但是tags如果有多个值,会生成多个list

    @Api(value="用户controller",tags={"用户操作接口"})
    @RestController
    public class UserController { }

     

     @ApiOperation() 

    用于方法;表示一个http请求的操作 
    value用于方法描述 
    notes用于提示内容 
    tags可以重新分组(视情况而用) 
    @ApiParam() 

    用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等) 
    name–参数名 
    value–参数说明 
    required–是否必填

    @Api(value="用户controller",tags={"用户操作接口"})
    @RestController
    public class UserController { 
    
        @ApiOperation(value="获取用户信息",tags={"获取用户信息copy"},notes="注意问题点") 
        @GetMapping("/getUserInfo")
        public User getUserInfo(@ApiParam(name="id",value="用户id",required=true) Long id,@ApiParam(name="username",value="用户名") String username) { 
            // userService可忽略,是业务逻辑 
            User user = userService.getUserInfo(); 
            return user; 
        } 
    }

     @ApiModel()

    用于类 ;表示对类进行说明,用于参数用实体类接收 

    value–表示对象名 
    description–描述 
    都可省略 
    @ApiModelProperty()

    用于方法,字段; 表示对model属性的说明或者数据操作更改 
    value–字段说明 
    name–重写属性名字 
    dataType–重写属性类型 
    required–是否必填 
    example–举例说明 
    hidden–隐藏

    @ApiModel(value="user对象",description="用户对象user")
    public class User implements Serializable{ 
        private static final long serialVersionUID = 1L; 
        @ApiModelProperty(value="用户名",name="username",example="xingguo") 
        private String username; 
        @ApiModelProperty(value="状态",name="state",required=true) 
        private Integer state; 
        private String password; 
        private String nickName; 
        private Integer isDeleted; 
        @ApiModelProperty(value="id数组",hidden=true) 
        private String[] ids; 
        private List<String> idList; 
        //省略get/set 
    }
    @ApiOperation("更改用户信息")
    @PostMapping("/updateUserInfo")
    public int updateUserInfo(@RequestBody @ApiParam(name="用户对象",value="传入json格式",required=true) User user){ 
        int num = userService.updateUserInfo(user); 
        return num; 
    }

     

    @ApiIgnore()

    用于类或者方法上,可以不被swagger显示在页面上 
    比较简单, 这里不做举例

    @ApiImplicitParam() 

    用于方法 
    表示单独的请求参数 
    @ApiImplicitParams() 

    用于方法,包含多个 @ApiImplicitParam 
    name–参数ming 
    value–参数说明 
    dataType–数据类型 
    paramType–参数类型 
    example–举例说明

    @ApiOperation("查询测试")
    @GetMapping("select")
    //@ApiImplicitParam(name="name",value="用户名",dataType="String", paramType = "query") 
    @ApiImplicitParams({ @ApiImplicitParam(name="name",value="用户名",dataType="string", paramType = "query",example="xingguo"), @ApiImplicitParam(name="id",value="用户id",dataType="long", paramType = "query")}) 
    public void select(){ }

     文章转载至:https://blog.csdn.net/wyb880501/article/details/79576784

  • 相关阅读:
    Oracle存储过程实现返回多个结果集 在构造函数方法中使用 dataset
    刷卡客户端实现
    winform TreeView 节点选择
    flask入门
    redis介绍及常见问题总结
    微信消息推送
    redis介绍及在购物车项目中的应用,用户认证
    django实现支付宝支付
    nginx+uWSGI+django部署web服务器
    linux上部署redis实现与Python上的redis交互(有坑)
  • 原文地址:https://www.cnblogs.com/nhdlb/p/14044969.html
Copyright © 2011-2022 走看看