Springboot整合swagger

      相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档。常用的工具有诸如小幺鸡、showdoc等,不过都需要人工进行编辑,大大影响了效率,或者是无法在线测试,接口更改后又得重新修改接口文档。

      Swagger也就是为了解决这个问题,当然也不能说Swagger就一定是完美的,当然也有缺点,最明显的就是代码移入性比较强。其他的不多说,想要了解Swagger的,可以去Swagger官网,可以直接使用Swagger editor编写接口文档,当然我们这里讲解的是SpringBoot整合Swagger2,直接生成接口文档的方式。
 

一、pom导包

<dependency>
			<groupId>com.spring4all</groupId>
			<artifactId>spring-boot-starter-swagger</artifactId>
			<version>1.5.1.RELEASE</version>
</dependency>

二、swagger配置编写,application.yml

swagger:
  # 是否启用swagger,默认:true
  enabled: true
  # 标题
  title: swagger-demo API 管理
  # 描述
  description: 这里是 swagger-demo API 管理的描述信息
  # 版本
  version: 0.0.1-SNAPSHOT
  # 许可证
  license: MIT License
  contact:
      # 维护人
      name: heaven
Spring Boot 整合 Swagger 的详细教程如下: ### 安装 Swagger 先下载 Swagger,然后完成安装操作,不过引用中未提及具体的下载安装步骤,一般在 Spring Boot 项目里可通过在 `pom.xml` 中添加 Swagger 相关依赖来完成安装,例如添加 Swagger2 Swagger UI 的依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` ### 使用 Swagger 1. **编写接口**:在 Spring Boot 项目里编写 RESTful 接口,示例代码如下: ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class HelloController { @GetMapping("/hello") public String hello() { return "Hello, Swagger!"; } } ``` 2. **启用 Swagger**:创建一个配置类来启用 Swagger,示例代码如下: ```java 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 api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot Swagger API") .description("API documentation for Spring Boot application") .version("1.0.0") .build(); } } ``` 3. **查看接口文档**:启动 Spring Boot 项目,在浏览器中访问 `http://localhost:8080/swagger-ui.html`(如果使用 Swagger2) 或者 `http://localhost:8080/doc.html`(如果使用 Swagger BootstrapUI),就能查看生成的接口文档 [^2]。 ### 高级使用 1. **描述数据模型**:借助 Swagger 的注解(如 `@ApiModel` `@ApiModelProperty`)来描述数据模型,示例代码如下: ```java import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel(description = "User model") public class User { @ApiModelProperty(value = "User ID", example = "1") private Long id; @ApiModelProperty(value = "User name", example = "John Doe") private String name; // Getters and setters } ``` 2. **描述枚举类型**:使用 `@ApiModel` `@ApiModelProperty` 注解描述枚举类型,示例代码如下: ```java import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; @ApiModel(description = "User role") public enum UserRole { @ApiModelProperty(value = "Admin role") ADMIN, @ApiModelProperty(value = "User role") USER } ``` 3. **描述响应参数**:使用 `@ApiResponse` 注解描述响应参数,示例代码如下: ```java import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Api(tags = "User API") @RestController @RequestMapping("/api/users") public class UserController { @ApiOperation(value = "Get user by ID") @ApiResponses(value = { @ApiResponse(code = 200, message = "Success", response = User.class), @ApiResponse(code = 404, message = "User not found") }) @GetMapping("/{id}") public User getUserById(Long id) { // Implementation return null; } } ``` ### 进阶使用 1. **配置全局参数**:若使用 Swagger BootstrapUI,可在“文档管理”中增加全局参数,包含添加 header 参数 [^3]。 2. **配置安全协议**:可在 Swagger配置安全协议,如 OAuth2 等。 3. **配置安全上下文**:配置安全上下文以确保接口的安全性。 4. **配置忽略参数**:配置忽略某些不需要显示的参数。 ### 整合 Spring Security 时的注意事项 在 Spring Boot 整合 Spring Security Swagger 时,需要配置拦截的路径放行的路径,要放行以下几个路径: ```java .antMatchers("/swagger**/**").permitAll() .antMatchers("/webjars/**").permitAll() .antMatchers("/v2/**").permitAll() .antMatchers("/doc.html").permitAll() ``` 如果使用了 bootstarp 的 Swagger UI 界面,需添加最后一个路径 [^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值