使用 Swagger 集成文档具有以下几个优势:
功能丰富 :支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能;
及时更新 :开发过程中花一点写注释的时间,就可以及时的更新API文档,省心省力;
整合简单 :通过添加pom依赖和简单配置,内嵌于应用中就可同时发布API接口文档界面,不需要部署独立服务。
本文主要和大伙来聊下在Spring Boot中如何整合Swagger2
1.创建springboot项目,导入maven坐标mysql,mybatis-plus,swagger,lombok,web
<dependencies>
//数据库连接驱动
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
//mybatisplus依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1.tmp</version>
</dependency>
//swagger相关依赖
<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>
//web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
//lombok主要少写get set方法
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
//测试依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.application.yml配置
datasource:
url: jdbc:mysql:///springboot?serverTimezone=UTC
username: root
password: 1231
server:
port: 8080
3.编写数据库实体类
package com.huanan.springbootswagger.entity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class User {
@ApiModelProperty(value = "用户id")
private Integer id;
@ApiModelProperty(value = "用户名")
private String username;
@ApiModelProperty(value = "用户手机")
private String phone;
@ApiModelProperty(value = "用户性别")
private String sex;
}
4.swagger配置
开发者自己提供一个Docket的Bean
package com.huanan.springbootswagger.config;
import com.google.common.base.Predicates;
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.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 //swagger注解
public class SwaggerConfig {
@Bean
public Docket webApiConfig() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
.paths(Predicates.not(PathSelectors.regex("/admin/.*")))
.paths(Predicates.not(PathSelectors.regex("/error.*")))
.build();
}
private ApiInfo webApiInfo() {
return new ApiInfoBuilder()
.title("网站-课程中心API文档")
.description("本文档描述了课程中心微服务接口定义")
.version("1.0")
.contact(new Contact("java", "http://baidu.com", "1578689361@qq.com"))
.build();
}
}
5.编写mapper层
package com.huanan.springbootswagger.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huanan.springbootswagger.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
6.编写controller层 这里我只用了增加和查询所有
package com.huanan.springbootswagger.controller;
import com.huanan.springbootswagger.entity.User;
import com.huanan.springbootswagger.mapper.UserMapper;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@Api(tags = "用户管理相关接口")
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
//查询所有
@GetMapping("/findAll")
public List<User> findAll(){
List<User> users = userMapper.selectList(null);
return users;
}
//添加用户
@PostMapping("/addUser")
public boolean addUser(@RequestBody User user){
int insert = userMapper.insert(user);
if (insert>=0){
return true;
}
else {
return false;
}
}
}
7.启动项目访问http://localhost:8080/swagger-ui.html
接着点开用户管理相关接口
看到数据证明测试成功!!!