• springboot整合swagger


    一.项目搭建

    项目地址:https://github.com/Rong0912/Springboot_swagger

    第一步:新建一个springboot项目然后添加必要的依赖

    <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>
    <dependency>
        <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    第二步:添加model层和controller层

    User.java

    @ApiModel
    public class User implements Serializable {
        @ApiModelProperty(value = "用户id")
        private Integer id;
        @ApiModelProperty(value = "用户名")
        private String username;
        @ApiModelProperty(value = "用户地址")
        private String address;
    
        //setter和getter方法
        }

    UserController.java

    @RestController
    @Api(tags = "用户管理相关接口")
    @RequestMapping("/user")
    public class UserController {
    
        @GetMapping("/")
        @ApiOperation("根据id查询用户的接口")
        @ApiImplicitParam(name = "id", value = "用户id", defaultValue = "99", required = true)
        public User getUserById(@PathVariable Integer id) {
            User user = new User();
            user.setId(id);
            return user;
        }
      @PostMapping("/")
      @ApiOperation("添加用户的接口")
      @ApiImplicitParams({
       @ApiImplicitParam(name = "username", value = "用户名", defaultValue = "李四"),
      @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "深圳", required = true)
      }
      )
      public User addUser(String username, @RequestParam(required = true) String address) {
       return new User();
      }
    }

    第三步:添加配置类(重点)

    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .pathMapping("/")
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.swagger.controller"))
                    .paths(PathSelectors.any())
                    .build().apiInfo(new ApiInfoBuilder()
                            .title("SpringBoot整合Swagger")
                            .description("SpringBoot整合Swagger,详细信息......")
                            .version("9.0")
                            .contact(new Contact("啊啊啊啊","blog.csdn.net","aaa@gmail.com"))
                            .license("The Apache License")
                            .licenseUrl("http://www.baidu.com")
                            .build());
        }
    }

      通过@Configuration注解,让Spring-boot来加载该类配置。再通过@EnableSwagger2注解来启用Swagger2Configuration。Swagger会扫描该包下所有Controller定义的API,并产生文档内容(除了被@ApiIgnore指定的请求)

      若自己创建项目,注意修改SwaggerConfig类中的“com.example.swagger.controller”,改成自己controller层的Reference

    第五步:启动应用,浏览器请求

    http://localhost:8080/swagger-ui.html

    二.swagger2 注解整体说明

    1. @Api:用在请求的类上,说明该类的作用(一般写在controller类的上方)

    @Api:用在请求的类上,说明该类的作用
        tags="说明该类的作用"
        value="该参数没什么意义,所以不需要配置"

     实例:@Api(tags = "用户管理相关接口")

    2. @ApiOperation:用在请求的方法上,说明方法的作用

    @ApiOperation:"用在请求的方法上,说明方法的作用"
        value="说明方法的作用"
        notes="方法的备注说明"
    实例:@ApiOperation("根据id查询用户的接口")

    3. @ApiImplicitParams:用在请求的方法上,包含一组参数说明

    @ApiImplicitParams:用在请求的方法上,包含一组参数说明
        @ApiImplicitParam:用在 @ApiImplicitParams 注解中,指定一个请求参数的配置信息       
            name:参数名
            value:参数的汉字说明、解释
            required:参数是否必须传
            paramType:参数放在哪个地方
                · header --> 请求参数的获取:@RequestHeader
                · query --> 请求参数的获取:@RequestParam
                · path(用于restful接口)--> 请求参数的获取:@PathVariable
                · body(不常用)
                · form(不常用)    
            dataType:参数类型,默认String,其它值dataType="Integer"       
            defaultValue:参数的默认值

    实例:

    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", defaultValue = "李四"),
            @ApiImplicitParam(name = "address", value = "用户地址", defaultValue = "深圳", required = true)
    }
    )

    4. @ApiResponses:用于请求的方法上,表示一组响应

    @ApiResponses:用于请求的方法上,表示一组响应
        @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
            code:数字,例如400
            message:信息,例如"请求参数没填好"
            response:抛出异常的类

    实例:

    @ApiOperation(value = "select1请求",notes = "多个参数,多种的查询参数类型")
    @ApiResponses({
        @ApiResponse(code=400,message="请求参数没填好"),
        @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
    })

    5. @ApiModel:用于响应类上,表示一个返回响应数据的信息

    @ApiModel:用于响应类上,表示一个返回响应数据的信息
                (这种一般用在post创建的时候,使用@RequestBody这样的场景,
                请求参数无法使用@ApiImplicitParam注解进行描述的时候)
        @ApiModelProperty:用在属性上,描述响应类的属性

    实例

    @ApiModel
    public class User implements Serializable {
        @ApiModelProperty(value = "用户id")
        private Integer id;
        @ApiModelProperty(value = "用户名")
        private String username;
        @ApiModelProperty(value = "用户地址")
        private String address;
    }
  • 相关阅读:
    python运行出现TypeError: super() takes at least 1 argument (0 given)错误
    python使用Pyinstaller打包
    python 将字符串中的unicode字符码转换成字符
    python 复制列表
    AetherUpload大文件传输
    phpstom激活
    BusyBox telnetd配置
    MDK链接脚本错误
    利用mass storage class 做免驱动usb设备.
    BMP图片的解析,关于压缩方式
  • 原文地址:https://www.cnblogs.com/rong0912/p/12049978.html
Copyright © 2020-2023  润新知