强烈推荐一个大神的人工智能的教程:http://www.captainbed.net/zhanghan

【前言】

前后端分离是现在系统的主流,前端人员更多专注于前端功能,后端人员更加关注后端极大提高开发效率;一般情况下前后端由不同的开发团队进行开发;所以免不了要有一份接口文档,手写接口文档,维护接口文档团队间沟通,调试等也是需要花费一定的时间,Swagger就在一定程度上解决了以上问题;今天将自己的项目集成Swagger;

【集成Swagger之路】

         一、Springboot集成Swagger的方式

1、Pom中增加相关依赖

        <!-- swagger --><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>${springfox-swagger2.version}</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>${springfox-swagger-ui.version}</version></dependency>

2、增加Swagger配置类

package com.zhanghan.zhboot.config;import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
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;/*** Swagger2配置类* 在与spring boot集成时,放在与Application.java同级的目录下。* 通过@Configuration注解,让Spring来加载该类配置。* 再通过@EnableSwagger2注解来启用Swagger2。*/
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {//定义扫描的controller的路径private final static String controllerPath = "com.zhanghan.zhboot.controller";/*** 创建API应用* apiInfo() 增加API相关信息* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,* 本例采用指定扫描的包路径来定义指定要建立API的目录。** @return*/@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage(controllerPath)).paths(PathSelectors.any()).build();}/*** 创建该API的基本信息(这些基本信息会展现在文档页面中)* 访问地址:http://项目实际地址/swagger-ui.html** @return*/private ApiInfo apiInfo() {return new ApiInfoBuilder().title("springboot集成swagger").description("简单优雅的restfun风格,https://blog.csdn.net/zhanghan18333611647").termsOfServiceUrl("https://blog.csdn.net/zhanghan18333611647").version("1.0").build();}}

3、application中增加Swagger开关(此开关为true则代表启用Swagger;false则不启用Swagger;一般为安全起见生产环境置为false)

#****************************swagger***************************
#true is display swagger; false not disply swagger
swagger.enable=true

4、controller中增加相关的Swagger描述

package com.zhanghan.zhboot.controller;import com.mysql.jdbc.StringUtils;
import com.zhanghan.zhboot.controller.request.MobileCheckRequest;
import com.zhanghan.zhboot.properties.MobilePreFixProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;import java.util.HashMap;
import java.util.Map;@RestController
@Api(value = "校验手机号控制器",tags = {"校验手机号控制器"})
public class CheckMobileController {@Autowiredprivate MobilePreFixProperties mobilePreFixProperties;@ApiOperation(value="优雅校验手机号格式方式",tags = {"校验手机号控制器"})@RequestMapping(value = "/good/check/mobile", method = RequestMethod.POST)public Map goodCheckMobile(@RequestBody @Validated MobileCheckRequest mobileCheckRequest) {String countryCode = mobileCheckRequest.getCountryCode();String proFix = mobilePreFixProperties.getPrefixs().get(countryCode);if (StringUtils.isNullOrEmpty(proFix)) {return buildFailResponse();}String mobile = mobileCheckRequest.getMobile();Boolean isLegal = false;if (mobile.startsWith(proFix)) {isLegal = true;}Map map = new HashMap();map.put("code", 0);map.put("mobile", mobile);map.put("isLegal", isLegal);map.put("proFix", proFix);return map;}@ApiOperation(value="扩展性差校验手机号格式方式",tags = {"校验手机号控制器"})@RequestMapping(value = "/bad/check/mobile", method = RequestMethod.POST)public Map badCheckMobile(@RequestBody MobileCheckRequest mobileCheckRequest) {String countryCode = mobileCheckRequest.getCountryCode();String proFix;if (countryCode.equals("CN")) {proFix = "86";} else if (countryCode.equals("US")) {proFix = "1";} else {return buildFailResponse();}String mobile = mobileCheckRequest.getMobile();Boolean isLegal = false;if (mobile.startsWith(proFix)) {isLegal = true;}Map map = new HashMap();map.put("code", 0);map.put("mobile", mobile);map.put("isLegal", isLegal);map.put("proFix", proFix);return map;}private Map buildFailResponse() {Map map = new HashMap();map.put("code", 1);map.put("mobile", "");map.put("isLegal", false);map.put("proFix", "");return map;}}

5、请求实体中增加相关的Swagger描述

package com.zhanghan.zhboot.controller.request;import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;import javax.validation.constraints.NotNull;@ApiModel("手机号校验请求实体")
@Data
public class MobileCheckRequest {@ApiModelProperty(value = "国家编码",required = true)@NotNullprivate String countryCode;@ApiModelProperty(value = "手机号",required = true)@NotNullprivate String mobile;}

         二、效果展示

1、启动项目访问地址:http://localhost:8080/swagger-ui.html

2、Try it out

3、Execute

         三、项目地址及代码版本:

1、地址:https://github.com/dangnianchuntian/springboot

2、代码版本:1.1.0-Release

【总结】

1、充分利用好工具提高效率;

2、小工具有大用处,多去研究。

SpringBoot实战(八):集成Swagger相关推荐

  1. SpringBoot 实战 (八) | 使用 Spring Data JPA 访问 Mysql 数据库

    微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 如题,今天介绍 Spring Data JPA 的使用. 什么是 Spring Data JPA 在介绍 Spri ...

  2. SpringBoot实战之集成JSP模板展示(三)

    集成jsp数据展示 1.引入依赖 <dependency><groupId>jstl</groupId><artifactId>jstl</art ...

  3. Spring Boot集成Swagger

    Spring Boot集成Swagger @(Swagger)[swagger, springfox, springboot] Spring Boot集成Swagger 前言 基本概述 案例 引入依赖 ...

  4. gRPC学习之六:gRPC-Gateway集成swagger

    欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos gRPC学习系列文章链接 在CentOS7部署和设置G ...

  5. 亲测简单易懂可用:阿里云OSS入门实战2(集成到SpringBoot项目中存放用户头像)

    亲测简单易懂可用:阿里云OSS入门实战2(集成到SpringBoot项目中存放用户头像) 大噶好,我们继续延续上一章,学习如何使用OSS存放用户头像代码示例; 在application.propert ...

  6. SpringBoot教程(十六) | SpringBoot集成swagger(全网最全)

    一. 接口文档概述 swagger是当下比较流行的实时接口文文档生成工具.接口文档是当前前后端分离项目中必不可少的工具,在前后端开发之前,后端要先出接口文档,前端根据接口文档来进行项目的开发,双方开发 ...

  7. MP实战系列(二)之集成swagger

    其实与spring+springmvc+mybatis集成swagger没什么区别,只是之前写的太不好了,所以这次决定详细写. 提到swagger不得不提rest,rest是一种架构风格,里面有对不同 ...

  8. springboot 集成 swagger 自动生成API文档

    Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化RESTful风格的Web服务.简单来说,Swagger是一个功能强大的接口管理工具,并且提供了多种编程语言的前后端分离解决方案. S ...

  9. [Swagger2]SpringBoot集成Swagger

    SpringBoot集成Swagger 引入依赖 <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 ...

最新文章

  1. Windows azure 上将Legcay IO操作轻松切换到Blob
  2. Geotools简介以及quickstsrt加载shp文件并显示
  3. linux字体如何删除不了,如何彻底替换Ubuntu下Chrome字体(清除楷体字)
  4. Hive 之 分析窗口函数
  5. 跨境电商为什么需要ERP系统?
  6. Go语言并发机制初探
  7. 1、分组选择器, 2、尺寸 (Dimension)属性,3、Display(显示) 与 Visibility(可见性),4、CSS Display - 块和内联元素,5、CSS Position(定位
  8. N54L文件服务器,N54L安装群晖需要修改的硬件设置
  9. “零基础圆梦华为RS HCNP”视频课程规划
  10. 资本市场律师David Cameron作为合伙人加入德汇律师事务所香港办事处
  11. TCP SYN握手报文可以传输数据吗
  12. 计算机类毕业设计评语导师评语,毕业设计导师评语
  13. http://www.jobui.com/mianshiti/it/java/6782/
  14. 酒旅江湖战事:携程坚挺,美团蓄力,抖音来战
  15. 【001】Zabbix学习笔记-Zabbix简介与部署
  16. 谷歌浏览器插件打包ChromePackage-extention
  17. 批量安装Windows系统
  18. java int类型能表示的数值范围
  19. ホワイトブレス 初回版
  20. python快速实现简易中国象棋游戏

热门文章

  1. 微软低头,Chrome 一统浏览器!
  2. App 开发穷途末路?
  3. Google 重返中国的忧虑
  4. JavaScript 详解:为什么写好的代码非常重要
  5. 说好的 All in AI,没想到科技巨头们都开始偷偷的挖矿中!
  6. ======第五章设备管理======
  7. 数据流中的中位数 c语言,41 数据流中的中位数(时间效率)
  8. mysql通配符like,不吃透都对不起自己
  9. 差点无缘Offer!java开发和运行环境实验报告
  10. problem making ssl connection