输入以上最简配置,即可在spring中使用swagger。
打开页面:localhost:8080/swagger-ui.html,即可看到如下的页面,包括了四部分。接下来解析这四个模块的作用和配置。
如果不涉及开发环境,测试环境,线上环境的话,下面的代码可以直接用。
package com.salong.swaggerdemo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; 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; import java.util.ArrayList; @Configuration @EnableSwagger2 //开启swagger2 public class swaggerConfig { //配置swagger的bean实例 @Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(getApiInfo()) .select() //apis方法是配置要扫描的接口的方式 // basePackage是指定路径扫描方式 //any():全部扫描,none()全不扫描 //withClassAnnotation,只扫描类上有指定的注解的接口 //withMethodAnnotation,只扫描方法上有指定的注解的接口 .apis(RequestHandlerSelectors.basePackage("com.salong.swaggerdemo.controller")) //paths方法配置过滤不扫描的文件路径 .paths(PathSelectors.any()) .build(); } //配置swagger信息(网页左上角模块) private ApiInfo getApiInfo(){ //contact包含了作者信息 Contact contact=new Contact( "Salong", "https://blog.csdn.net/qq_35429398", "salong0503@aliyun.com"); return new ApiInfo( "Salong的API文档", "以梦为马,不负韶华!", "1.0", "https://blog.csdn.net/qq_35429398", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }作用:只想要在测试环境和开发环境中使用,线上环境不使用swagger。
完整swagger代码如下:
package com.salong.swaggerdemo.config; 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 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; import java.util.ArrayList; @Configuration @EnableSwagger2 //开启swagger2 public class swaggerConfig { //配置swagger的bean实例 @Bean public Docket docket(Environment environment){ //设置要显示swagger的环境 Profiles profiles=Profiles.of("dev","test"); //获取项目的环境,通过environment.acceptsProfiles来判断 //目前环境是否是处在自己设定的环境中 boolean flag=environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(getApiInfo()) .enable(flag) //是否启用swagger,如果flag为false,则浏览器不能看到 .select() //apis方法是配置要扫描的接口的方式 // basePackage是指定路径扫描方式 //any():全部扫描,none()全不扫描 //withClassAnnotation,只扫描类上有指定的注解的接口 //withMethodAnnotation,只扫描方法上有指定的注解的接口 .apis(RequestHandlerSelectors.basePackage("com.salong.swaggerdemo.controller")) //paths方法配置过滤不扫描的文件路径 .paths(PathSelectors.any()) .build(); } //配置swagger信息(网页左上角模块) private ApiInfo getApiInfo(){ //contact包含了作者信息 Contact contact=new Contact( "Salong", "https://blog.csdn.net/qq_35429398", "salong0503@aliyun.com"); return new ApiInfo( "Salong的API文档", "以梦为马,不负韶华!", "1.0", "https://blog.csdn.net/qq_35429398", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }其中,为了获取当前的环境,需要在application.yml配置文件中加入下面的代码,当此参数和swagger设置的参数一样时候,swagger 就可以被浏览器查看到。
spring: profiles: active: pro