SpringBoot gradle项目集成swagger

新建基于gradle的spring boot项目

勾选spring web模块即可

SpringBoot gradle项目集成swagger_第1张图片

build.gradle文件添加gradle依赖

dependencies {
      compile "io.springfox:springfox-swagger2:2.9.2"
      compile "io.springfox:springfox-swagger-ui:2.9.2"
}

创建Swagger2的配置类SwaggerConfig

package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

  @Bean
  public Docket documentation() {
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
      .apis(RequestHandlerSelectors.basePackage("com.example.demo")).paths(PathSelectors.any()).build();
  }

  private ApiInfo apiInfo() {
    return new ApiInfoBuilder().title("API SWAGGER").description("SWAGGER 説明 DOCUMENT").version("1.0").build();
  }
}

记得@Configuration注解,通过@Configuration注解,让Spring来加载该类配置。再通过@EnableSwagger2注解来启用Swagger2。

com.example.demo待扫描的接口包,Swagger会扫描该包下所有Controller定义的API ,并生成API文档(除了被@ApiIgnore指定的请求)。

Swagger常用注解
@Api()用于类
@ApiOperation()用于方法
@ApiParam()用于方法,参数,字段说明
@ApiModel()用于类
@ApiModelProperty()用于方法,字段
@ApiIgnore()用于类,方法,方法参数, 表示这个方法或者类被忽略
@ApiImplicitParam() 用于方法
@ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
@ApiResponse

HelloController.java

package com.example.demo.controller;

import com.example.demo.dto.DemoRequestDto;
import com.example.demo.dto.DemoResponseDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "世界")
public class HelloController {

    @GetMapping("hello")
    @ApiOperation(value = "查找信息", notes = "根据Id查找学生信息")
    public DemoResponseDto hello(DemoRequestDto request) {
        DemoResponseDto response = new DemoResponseDto();
        return response;
    }
}

DemoRequestDto.java

package com.example.demo.dto;

import io.swagger.annotations.ApiModelProperty;

public class DemoRequestDto {

    @ApiModelProperty(value = "ID")
    private Integer id;
    @ApiModelProperty(value = "名称")
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

DemoResponseDto.java

package com.example.demo.dto;

import io.swagger.annotations.ApiModelProperty;

public class DemoResponseDto {

    @ApiModelProperty(value = "学校")
    private String school;
    @ApiModelProperty(value = "班级ID")
    private String classId;
    @ApiModelProperty(value = "班级名称")
    private String className;

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    public String getClassId() {
        return classId;
    }

    public void setClassId(String classId) {
        this.classId = classId;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }
}

启动SpringBoot程序,访问:http://localhost:8080/swagger-ui.html

SpringBoot gradle项目集成swagger_第2张图片

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(SpringBoot,gradle,swagger)