SpringBoot之集成swagger2

maven配置

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.5.0</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.5.0</version>
</dependency>

 

 

增加Swagger2Config.java配置文件
package com.zns.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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {

    //是否线上环境 可以通过文件配置是否线上环境 此处直接写死
    private Boolean isOnLineFlag = false;

    @Bean
    public Docket createRestApi() {
        if (isOnLineFlag) {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfoOnline())
                    .select()
                    .paths(PathSelectors.none())//如果是线上环境,添加路径过滤,设置为全部都不符合
                    .build();
        } else {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.zns"))
                    .paths(PathSelectors.any())
                    .build();
        }
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                //创建人
                .contact(new Contact("xxx", "http://www.xxx.com", ""))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }

    private ApiInfo apiInfoOnline() {
        return new ApiInfoBuilder()
                .title("")
                .description("")
                .license("")
                .licenseUrl("")
                .termsOfServiceUrl("")
                .version("")
                .contact(new Contact("", "", ""))
                .build();
    }
}

 

 

增加StudentController测试

package com.zns.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "student", description = "学生", tags = {"Student"})
@RestController
public class StudentController {

    @ApiOperation(value = "根据id查询学生信息", notes = "根据id查询学生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生ID", defaultValue = "1", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "/get", method = RequestMethod.POST)
    public Object get(Integer id) {
        return "xxx";
    }
}

 

启动项目访问接口文档

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

 

上一篇:springboot整合Swagger2


下一篇:SpringBoot整合Swagger2