随着互联网的发展,技术越来越成熟,网站架构从开始的前后端一起变成了前后端分离,这样前后端就得靠着 Api 文档
联系,大量的接口文档编写以及修改会占用开发的时间,所以项目中往往会引入 Swagger 这类 Api 开发框架,更加方
便的生成和管理 Api 文档,并且 Swagger 生成的 Api 文档还会随着接口更改而相应发生改变。
GitHub地址:https://github.com/intomylife/SpringBoot
DROP TABLE IF EXISTS `springboot_swagger` ;
CREATE TABLE `springboot_swagger` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID',
`type` varchar(2) DEFAULT NULL COMMENT '生活用品类别:1. 家电类 2. 厨具',
`name` varchar(50) DEFAULT NULL COMMENT '生活用品名称',
`description` varchar(200) DEFAULT NULL COMMENT '生活用品描述',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='springboot整合swagger测试表';
INSERT INTO springboot_swagger ( type , name , description )
VALUES ('1','电饭煲','用来蒸饭'),('1','电热壶','用来烧水'),('1','空调','用来制冷或制热'),('2','菜刀','用来切菜'),('2','刨子','用来剥皮'),('2','打蛋器','用来搅拌鸡蛋');
4.0.0
com.zwc
springboot-swagger-commons
0.0.1-SNAPSHOT
springboot-swagger-commons
公用工程
jar
UTF-8
1.8
Cairo-SR3
1.1.9
1.2.47
1.9.9
3.0-RELEASE
1.3.2
1.1.0
2.7.0
com.alibaba
druid-spring-boot-starter
${druid.version}
mysql
mysql-connector-java
com.alibaba
fastjson
${fastjson.version}
org.codehaus.jackson
jackson-mapper-asl
${jackson.mapper.asl.version}
org.mybatis.spring.boot
mybatis-spring-boot-starter
${mybatis-spring-boot-starter.version}
com.baomidou
mybatis-plus-boot-starter
${mybatis-plus-boot-starter.version}
org.mybatis.caches
mybatis-ehcache
${mybatis.ehcache.version}
io.springfox
springfox-swagger2
${springfox.version}
io.springfox
springfox-swagger-ui
${springfox.version}
io.spring.platform
platform-bom
${platform-bom.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
# mybatis-plus
## 扫描 mapper 文件
mybatis-plus.mapper-locations=classpath*:com/zwc/*/mapper/xml/*.xml
## 扫描实体类
mybatis-plus.type-aliases-package=com.zwc.*.domain
package com.zwc.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
/*
* @ClassName MyBatisPlusConfig
* @Desc TODO mybatis-plus 配置
* @Date 2019/4/3 14:31
* @Version 1.0
*/
@Configuration
@PropertySource("classpath:system.properties")
public class MyBatisPlusConfig {
/*
* @ClassName MyBatisPlusConfig
* @Desc TODO mybatis-plus 配置拦截
* @Date 2019/3/26 18:13
* @Version 1.0
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
// 设置方言
paginationInterceptor.setDialectType("mysql");
return paginationInterceptor;
}
}
package com.zwc.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
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;
/**
* @ClassName SwaggerConfig
* @Desc TODO swagger 配置
* @Date 2019/4/3 10:33
* @Version 1.0
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/*
* @ClassName SwaggerConfig
* @Desc TODO swagger 配置信息
* @Date 2019/4/3 11:00
* @Version 1.0
*/
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) // swagger 网页描述信息
.enable(true) // 是否开启
.select() // 选择那些路径和 api 会生成 document
.apis(RequestHandlerSelectors.basePackage("com.zwc")) // com.zwc 包下的 api 会生成 document
.paths(PathSelectors.any()) // 表示所有路径都符合。PathSelectors.none() 表示所有路径都不符合
.build();
}
/*
* @ClassName SwaggerConfig
* @Desc TODO swagger 网页描述信息
* @Date 2019/4/3 10:40
* @Version 1.0
*/
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("springboot 整合 swagger") // 标题
.description("使用 springboot 整合 swagger 来构建 api 文档") // 描述
.version("1.0.1") // 版本
.build();
}
}
service 工程是一个父工程,里面可能会包含 基础模块,用户模块,订单模块等等... 每个模块中又会分为 core 和 api
# 端口
server.port=8082
# 数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/base_db?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&serverTimezone=PRC&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
# 打印 sql 日志
logging.level.com.zwc.base.mapper=debug
package com.zwc.base.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
/*
* @ClassName SpringBootSwagger
* @Desc TODO springboot 整合 swagger 测试表
* @Date 2019/4/3 11:07
* @Version 1.0
*/
@Data
@ApiModel("springboot 整合 swagger 测试表")
public class SpringbootSwagger extends Model {
private static final long serialVersionUID = -7876888313791106541L;
/**
* 自增ID
*/
@ApiModelProperty(value = "主键")
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* 生活用品类别:1. 家电类 2. 厨具
*/
@ApiModelProperty(value = "生活用品类别:1. 家电类 2. 厨具")
private String type;
/**
* 生活用品名称
*/
@ApiModelProperty(value = "生活用品名称")
private String name;
/**
* 生活用品描述
*/
@ApiModelProperty(value = "生活用品描述")
private String description;
public static final String ID = "id";
public static final String TYPE = "type";
public static final String NAME = "name";
public static final String DESCRIPTION = "description";
@Override
protected Serializable pkVal() {
return this.id;
}
}
注:还有相关的扩展实体类这里不一一展示占篇幅了,具体代码可以在 Github 中获取
package com.zwc.base.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zwc.base.dto.request.SpringbootSwaggerRequestAddDTO;
import com.zwc.base.dto.request.SpringbootSwaggerRequestDTO;
import com.zwc.base.dto.request.SpringbootSwaggerRequestQueryDTO;
import com.zwc.base.dto.response.SpringbootSwaggerResponseDTO;
import com.zwc.base.service.SpringbootSwaggerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
/**
* @ClassName SpringbootSwaggerController
* @Desc TODO springboot 整合 swagger 测试表 前端控制器
* @Date 2019/4/3 13:48
* @Version 1.0
*/
@RestController
@RequestMapping(value = "swagger") // , produces = MediaType.APPLICATION_JSON_VALUE
@Api(value = "springboot 整合 swagger 测试表 前端控制器" , description = "springboot 整合 swagger 测试表 api")
// 此注解用在类上整个类都不会出现在 swagger 里;用在此类方法上,此类该方法不会出现在 swagger 里,其他方法依旧会出现在 swagger 里
// @ApiIgnore
public class SpringbootSwaggerController {
@Autowired
private SpringbootSwaggerService springbootSwaggerService;
/*
* @ClassName SpringbootSwaggerController
* @Desc TODO 新增信息
* @Date 2019/4/3 14:38
* @Version 1.0
*/
@RequestMapping(value = "/add" , method = RequestMethod.POST)
@ApiOperation(value = "新增信息")
public String add(SpringbootSwaggerRequestAddDTO springbootSwaggerRequestAddDTO){
return springbootSwaggerService.add(springbootSwaggerRequestAddDTO);
}
/*
* @ClassName SpringbootSwaggerController
* @Desc TODO 根据 id 删除信息 , 使用 @ApiParam 注解添加字段的描述,参数是否必填以 @ApiParam 注解中的 required 值为准
* @Date 2019/4/3 14:56
* @Version 1.0
*/
@RequestMapping(value = "/delete/{id}" , method = RequestMethod.GET)
@ApiOperation(value = "根据 id 删除信息")
public String delete(@ApiParam(value = "主键 id" , required = true) @PathVariable(value = "id" , required = true) long id){
return springbootSwaggerService.delete(id);
}
/*
* @ClassName SpringbootSwaggerController
* @Desc TODO 更新信息
* @Date 2019/4/3 15:03
* @Version 1.0
*/
@RequestMapping(value = "/update" , method = RequestMethod.POST)
@ApiOperation(value = "更新信息")
public String update(SpringbootSwaggerRequestDTO springbootSwaggerRequestDTO){
return springbootSwaggerService.update(springbootSwaggerRequestDTO);
}
/*
* @ClassName SpringbootSwaggerController
* @Desc TODO 分页查询信息
* @Date 2019/4/3 14:02
* @Version 1.0
*/
@RequestMapping(value = "/pageUser" , method = RequestMethod.POST)
@ApiOperation(value = "分页查询信息")
public Page pageUser(SpringbootSwaggerRequestQueryDTO springbootSwaggerRequestQueryDTO){
return springbootSwaggerService.getDataByPage(springbootSwaggerRequestQueryDTO);
}
}
到此 SpringBoot 整合 Swagger 就结束了,注意注解的使用,多多尝试,一定会成功的!
希望能够帮助到你
over