1、Springboot集成使用swagger2
1.1导入相关依赖
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency>
1.2配置
在config.SwaggerConfig下配置swagger
import org.springframework.context.annotation.Configuration;import springfox.documentation.swagger2.annotations.EnableSwagger2;/*** @author cyx* @create 2021-01-11 11:12*/@Configuration@EnableSwagger2 //开启swagger2public class SwaggerConfig {}
1.3测试访问
http://localhost:8080/swagger-ui.html
2、配置Swagger信息
package com.kuang.swagger.config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import springfox.documentation.builders.ApiInfoBuilder;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 //开启Swagger2public class SwaggerConfig {@Beanpublic Docket createRestApi(){return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());}//配置Swagger 信息=apiInfoprivate ApiInfo apiInfo() {//作者信息Contact contact = new Contact("cyx","1","807357194@qq.com");return new ApiInfoBuilder().title("cyx的SwaggerAPI文档").description("Swagger的API配置").termsOfServiceUrl("https://blog.csdn.net/gjs935219").contact(contact).version("v1.0").build();}
Swagger配置扫描接口
@Beanpublic Docket docket(){Docket build = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()//RequestHandlerSelectors:配置要扫描接口的方式//basePackage:指定要扫描的包//any():扫描全部//none():不扫描//withClassAnnotation:扫描类上的注解,参数是一个注解的反射对象//withMethodAnnotation:扫描方法上的注解.apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))//path():过滤什么路径.paths(PathSelectors.ant("/cyx/**")).build(); return build;}
配置是否启动swagger
//配置了Swagger的docket的bean实例@Beanpublic Docket docket(){Docket build = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(false)//enable是否启动swagger,如果为false,则swagger不能在浏览器中访问.select().apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))// .paths(PathSelectors.ant("/cyx/**")).build(); return build;}
只希望Swagger在生产环境中使用,在发布的时候不使用???
- 判断是不是生产环境,flag=false
- 注入enable(flag)
一个开发环境,一个开发完成后发布的环境


package com.cyx.swaggerdemo.config;import com.cyx.swaggerdemo.controller.HelloController;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.env.Environment;import org.springframework.core.env.Profiles;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestMapping;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;/*** @author cyx* @create 2021-01-11 11:12*/@Configuration@EnableSwagger2 //开启swagger2public class SwaggerConfig {//配置了Swagger的docket的bean实例@Beanpublic Docket docket(Environment environment){//设置要显示的Swagger环境Profiles profiles = Profiles.of("dev","test");//通过environment.acceptsProfiles判断是否处在自己设定的环境中boolean flag = environment.acceptsProfiles(profiles);Docket build = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).enable(flag)//enable是否启动swagger,如果为false,则swagger不能在浏览器中访问.select().apis(RequestHandlerSelectors.basePackage("com.cyx.swaggerdemo.controller"))// .paths(PathSelectors.ant("/cyx/**")).build(); return build;}//配置Swagger信息=apiinfoprivate ApiInfo apiInfo(){//作者信息Contact contact = new Contact("cyx","1","807357194@qq.com");return new ApiInfoBuilder().title("cyx的SwaggerAPI文档").description("服务名:springcloudproducer").version("1.0").build();}}
配置API文档的分组
Docket(DocumentationType.SWAGGER_2).groupName("cyx")
如何配置多个分组:多个Docket实例即可
@Beanpublic Docket docket1(){return new Docket(DocumentationType.SWAGGER_2).groupName("A");}@Beanpublic Docket docket2(){return new Docket(DocumentationType.SWAGGER_2).groupName("B");}@Beanpublic Docket docket3(){return new Docket(DocumentationType.SWAGGER_2).groupName("C");}
实体类配置
package com.cyx.swaggerdemo.pojo;import io.swagger.annotations.Api;import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;/*** @author cyx* @create 2021-01-11 14:06*///@Api(注释)@ApiModel("用户实体类")public class User {@ApiModelProperty("用户名")public String username;@ApiModelProperty("密码")public String password;}
Controller配置
package com.cyx.swaggerdemo.controller;import com.cyx.swaggerdemo.pojo.User;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;/*** @author cyx* @create 2021-01-11 11:09*/@Api(tags = "这是测试")@RestControllerpublic class HelloController {@GetMapping(value = "/hello")public String hello(){return "hello";}//只要我们的接口中,返回值中存在实体类,他就会被扫描到swagger中@PostMapping(value = "/user")public User user(){return new User();}@ApiOperation("hello控制类")@GetMapping(value = "/hello2")public String hello2(@ApiParam("用户名") String username){return "hello" + username;}}
注释
作用范围 API 使用位置
对象属性 @ApiModelProperty 用在参数对象的字段上
协议集描述 @Api 用在Conntroller类上
协议描述 @ApiOperation 用在controller方法上
Response集 @ApiResponses 用在controller方法上
Response @ApiResponse 用在@ApiResponses里面
非对象参数集 @ApilmplicitParams 用在controller方法上
总结
1、我们可以通过Swagger给一些比较难理解的属性或者接口,增加注释信息
2、接口文档实时更新
3、可以在线测试
Swagger是一个优秀的工具,几乎所有的大公司都有使用它
【注意点】在正式发布的时候,关闭Swagger!!!出于安全考虑,而且节省运行的内存!!!
