SpringBoot集成swagger

it2023-05-02  79

1.创建一个spring web项目。

2.导入swagger相关依赖。

<!-- 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> <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency>

3.编写一个hello的controller接口方法。(非必要,测试用)

package com.salong.swaggerdemo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class helloController { @RequestMapping("/say") public String hello(){ return "hello"; } }

4.配置swagger的config文件。

package com.salong.swaggerdemo.config; import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 //开启swagger2 public class swaggerConfig { }

输入以上最简配置,即可在spring中使用swagger。

5.测试运行。

打开页面:localhost:8080/swagger-ui.html,即可看到如下的页面,包括了四部分。接下来解析这四个模块的作用和配置。

6.swagger配置代码(单个生产环境)。

如果不涉及开发环境,测试环境,线上环境的话,下面的代码可以直接用。

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()); } }

7.swagger配置(多个环境)

作用:只想要在测试环境和开发环境中使用,线上环境不使用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

 

最新回复(0)