Swagger 是什么?
Swagger 是一个 RESTFUL 接口的文档在线自动生成和功能测试的框架. 利用 swagger2 的注解可以快速的在项目中构建 API 接口文档, 并且提供了测试 API 的功能.
Springboot 整合 Swagger2
创建 Springboot 项目, 添加相关依赖
- <dependencies>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>2.7.0</version>
- </dependency>
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>2.7.0</version>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- </dependencies>
项目结构
Swagger2 配置类
- @Configuration
- @EnableSwagger2
- public class Swagger2Configration {
- /*
- * 创建 API 文档
- * */
- @Bean
- public Docket createRestApi(){
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- .select() .apis(RequestHandlerSelectors.basePackage("com.jotal.springboot06swagger2.Controller")) // 为该包下的类 API 文档
- .apis(RequestHandlerSelectors.withClassAnnotation(API.class))// 为有 @API 注解的类生成 API 文档
- .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) // 为有 @ApiOperation 注解的方法生成 API 文档
- .paths(PathSelectors.any())
- .build();
- }
- /*
- * API 文档展示信息
- * */
- private ApiInfo apiInfo(){
- return new ApiInfoBuilder()
- .title("Jotal 项目接口文档")
- .description("测试 api 文档")
- .contact(new Contact("jotal","jotalngu.github.io",null))
- .version("0.1")
- .build();
- }
- }
@Configuration 标注该类为配置类
@EnableSwagger2 启用 Swagger2
@Bean 将 Docket 的实例对象注入 Spring IoC 容器中
apis 为选择生成 API 文档的范围, 有三种方式:
生成指定包下面的类的 API 文档
生成有指定注解的类的 API 文档
生成有指定注解的方法的 API 文档
paths(PathSelectors.any()) 表示路径选择器匹配所有路径
实体类
- package com.jotal.springboot06swagger2.entity;
- import io.swagger.annotations.ApiModel;
- import io.swagger.annotations.ApiModelProperty;
- @ApiModel(value = "用户信息")
- public class User {
- @ApiModelProperty(value ="年龄",name = "age")
- private int age;
- private String name;
- //getter,setter,toString 和构造器
- }
控制类
- package com.jotal.springboot06swagger2.Controller;
- import com.jotal.springboot06swagger2.entity.User;
- import io.swagger.annotations.API;
- import io.swagger.annotations.ApiOperation;
- import org.springframework.Web.bind.annotation.GetMapping;
- import org.springframework.Web.bind.annotation.RequestMapping;
- import org.springframework.Web.bind.annotation.RestController;
- import java.util.ArrayList;
- import java.util.List;
- @RestController
- @RequestMapping("/testApi")
- @API(tags = "用户信息接口")
- public class UserController {
- @GetMapping("/getAllUser")
- @ApiOperation(value="查询所有", notes="查询所有用户信息")
- public List<User> getAllUser(){
- List<User> userList = new ArrayList<>();
- for(int i=0;i<4;i++){
- User user = new User();
- user.setAge(i);
- userList.add(user);
- }
- return userList;
- }
- }
测试
http://localhost:8080/swagger-ui.html
点击 Try it out! 就可以测试功能!
来源: http://www.bubuko.com/infodetail-3147459.html