simplemall项目前几篇回顾:

源码地址:https://github.com/backkoms/simplemall

前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,swagger就是一款让你更好的书写API文档的框架。 本实战案例中也引入swagger2作为API管理工具,下面罗列下swagger2+SpringBoot使用步骤。

SpringBoot集成Swagger2

第一步,pom配置

  1. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->

  2. <dependency>

  3.    <groupId>io.springfox</groupId>

  4.    <artifactId>springfox-swagger2</artifactId>

  5.    <version>2.6.1</version>

  6. </dependency>

  7. <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->

  8. <dependency>

  9.    <groupId>io.springfox</groupId>

  10.    <artifactId>springfox-swagger-ui</artifactId>

  11.    <version>2.6.1</version>

  12. </dependency>

第二步编写配置管理类Swagger2Config

  1. package com.simplemall.micro.serv.page;

  2. import org.springframework.context.annotation.Bean;

  3. import org.springframework.context.annotation.Configuration;

  4. import io.swagger.annotations.ApiOperation;

  5. import springfox.documentation.builders.ApiInfoBuilder;

  6. import springfox.documentation.builders.PathSelectors;

  7. import springfox.documentation.builders.RequestHandlerSelectors;

  8. import springfox.documentation.service.ApiInfo;

  9. import springfox.documentation.spi.DocumentationType;

  10. import springfox.documentation.spring.web.plugins.Docket;

  11. import springfox.documentation.swagger2.annotations.EnableSwagger2;

  12. /**

  13. * swagger2 configuration

  14. *

  15. * @author guooo

  16. *

  17. */

  18. @Configuration//SpringBoot启动时自动装载

  19. @EnableSwagger2//打开swagger2功能,缺失的话同样无法打开ui页面

  20. publicclassSwagger2Config{

  21.    @Bean

  22.    publicDocket createRestApi(){

  23.        returnnewDocket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()

  24.                .apis(RequestHandlerSelectors.basePackage("com.simplemall.micro.serv.page.api"))

  25.                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

  26.                .paths(PathSelectors.any())

  27.                .build();

  28.    }

  29.    privateApiInfo apiInfo(){

  30.        returnnewApiInfoBuilder().title("Front app Swagger apis").description("For micro-service 's app to use")

  31.                .version("V1.0").build();

  32.    }

  33. }

经过以上两步简单的配置后,可以直接进行接口代码的编写。

  1. @Api(value ="用户服务", tags ="用户服务接口")

  2. @RestController

  3. @RefreshScope// 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。需要重新触发加载动作可以使用POST方式请求/refresh接口,该接口位于spring-boot-starter-actuator依赖,调用前需添加否则404。

  4. publicclassAPIAccountController{

  5.    @ApiOperation(value ="用户登陆")

  6.    @RequestMapping(value ="acc/login", method ={RequestMethod.POST })

  7.    publicRestAPIResult<String> login(@ApiParam(value ="手机号")@RequestParam(required =true)String phone,

  8.            @ApiParam(value ="密码")@RequestParam(required =true)String password,HttpSession session){

  9.        RestAPIResult<String> restAPIResult =newRestAPIResult<>();

  10.        Account account = accountFeignClient.login(phone, password);

  11.    }

使用swagger进行API管理的话,对代码有一定的侵入性,这个需要考虑在内。之前也提到过几种在线API的管理方式,点击链接《介绍几款常用的在线API管理工具

使用SpringBoot技术,再以maven原始的方式引入swagger使用的话,远不如一个starter来的爽,这里介绍一个swagger-starter,可以更快捷的与spring boot集成使用。

swagger-spring-boot-starter应用

在pom.xml中引入依赖:【当前最新版本 1.7.0.RELEASE】

  1. <dependency>

  2.    <groupId>com.spring4all</groupId>

  3.    <artifactId>swagger-spring-boot-starter</artifactId>

  4.    <version>1.7.0.RELEASE</version>

  5. </dependency>

注意:从1.6.0开始,我们按Spring Boot官方建议修改了artifactId为swagger-spring-boot-starter,1.6.0之前的版本不做修改,依然为使用spring-boot-starter-swagger !

在应用主类中增加@EnableSwagger2Doc注解

  1. @EnableSwagger2Doc

  2. @SpringBootApplication

  3. publicclassBootstrap{

  4.    publicstaticvoid main(String[] args){

  5.        SpringApplication.run(Bootstrap.class, args);

  6.    }

  7. }

默认情况下就能产生所有当前Spring MVC加载的请求映射文档。

参数配置,配置示例

  1. swagger.enabled=true

  2. swagger.title=spring-boot-starter-swagger

  3. swagger.description=Starterfor swagger 2.x

  4. swagger.version=1.4.0.RELEASE

  5. swagger.license=ApacheLicense,Version2.0

  6. swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html

  7. swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger

  8. swagger.contact.name=didi

  9. swagger.contact.url=http://blog.didispace.com

  10. swagger.contact.email=dyc87112@qq.com

  11. swagger.base-package=com.didispace

  12. swagger.base-path=/**

  13. swagger.exclude-path=/error, /ops/**

  14. swagger.globalOperationParameters[0].name=name one

  15. swagger.globalOperationParameters[0].description=some description one

  16. swagger.globalOperationParameters[0].modelRef=string

  17. swagger.globalOperationParameters[0].parameterType=header

  18. swagger.globalOperationParameters[0].required=true

  19. swagger.globalOperationParameters[1].name=name two

  20. swagger.globalOperationParameters[1].description=some description two

  21. swagger.globalOperationParameters[1].modelRef=string

  22. swagger.globalOperationParameters[1].parameterType=body

  23. swagger.globalOperationParameters[1].required=false

  24. // 取消使用默认预定义的响应消息,并使用自定义响应消息

  25. swagger.apply-default-response-messages=false

  26. swagger.global-response-message.get[0].code=401

  27. swagger.global-response-message.get[0].message=401get

  28. swagger.global-response-message.get[1].code=500

  29. swagger.global-response-message.get[1].message=500get

  30. swagger.global-response-message.get[1].modelRef=ERROR

  31. swagger.global-response-message.post[0].code=500

  32. swagger.global-response-message.post[0].message=500post

  33. swagger.global-response-message.post[0].modelRef=ERROR

详细介绍可参考源码,地址:https://github.com/SpringForAll/spring-boot-starter-swagger。由于JDK代码编译版本的限制,JDK1.7是不支持的,可使用1.8

扩展阅读:

 

基于SpringCloud的Microservices架构实战案例-在线API管理的更多相关文章

  1. 基于SpringCloud的Microservices架构实战案例-配置文件属性内容加解密

    使用过SpringBoot配置文件的朋友都知道,资源文件中的内容通常情况下是明文显示,安全性就比较低一些.打开application.properties或application.yml,比如mysq ...

  2. 基于SpringCloud的Microservices架构实战案例-架构拆解

    自第一篇< 基于SpringCloud的Microservices架构实战案例-序篇>发表出来后,差不多有半年时间了,一直也没有接着拆分完,有如读本书一样,也是需要契机的,还是要把未完成的 ...

  3. 基于SpringCloud的Microservices架构实战案例

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  4. 基于SpringCloud的微服务架构实战案例项目,以一个简单的购物流程为示例

    QuickStart 基于SpringCloud体系实现,简单购物流程实现,满足基本功能:注册.登录.商品列表展示.商品详情展示.订单创建.详情查看.订单支付.库存更新等等. 每个业务服务采用独立的M ...

  5. 基于SpringCloud的微服务架构实战案例项目

    QuickStart 基于SpringCloud体系实现,简单购物流程实现,满足基本功能:注册.登录.商品列表展示.商品详情展示.订单创建.详情查看.订单支付.库存更新等等. github源码地址:h ...

  6. 几款常用的在线API管理工具(是时候抛弃office编写接口文档了)

    在项目开发过程中,总会涉及到接口文档的设计编写,之前使用的都是ms office工具,不够漂亮也不直观,变更频繁的话维护成本也更高,及时性也是大问题.基于这个背景,下面介绍几个常用的API管理工具,方 ...

  7. 你不得不知的几款常用的在线API管理工具

    在项目开发过程中,总会涉及到接口文档的设计编写,之前使用的都是ms office工具,不够漂亮也不直观,变更频繁的话维护成本也更高,及时性也是大问题.基于这个背景,下面介绍几个常用的API管理工具,方 ...

  8. 介绍几款常用的在线API管理工具

    在项目开发过程中,总会涉及到接口文档的设计编写,之前使用的都是ms office工具,不够漂亮也不直观,变更频繁的话维护成本也更高,及时性也是大问题.基于这个背景,下面介绍几个常用的API管理工具,方 ...

  9. 基于springCloud的分布式架构体系

    Spring Cloud作为一套微服务治理的框架,几乎考虑到了微服务治理的方方面面,之前也写过一些关于Spring Cloud文章,主要偏重各组件的使用,本次分享主要解答这两个问题:Spring Cl ...

随机推荐

  1. AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享

    原文:AY的Dapper研究学习-继续深入-C#开发-aaronyang技术分享 ====================www.ayjs.net       杨洋    wpfui.com      ...

  2. Win8Metro(C#)数字图像处理--2.23二值图像开运算

    原文:Win8Metro(C#)数字图像处理--2.23二值图像开运算  [函数名称] 二值图像开运算函数OpenOperateProcess(WriteableBitmap src) [算法说明 ...

  3. 你遗忘的都在这里—iOS常用类型方法笔记

    这些都是项目中常用但又常忘的方法,与大家分享一下. 一.NSString 创建字符串.  NSString *astring = @"This is a String!"; 创建空 ...

  4. Xdite:永葆热情的上瘾式学习法(套路王:每天总结自己,反省自己的作息规律,找到自己的幸运时间、幸运方法,倒霉时间、倒霉方法。幸运是与注意力挂钩的。重复才能让自己登峰造极,主动去掉运气部分来训练自己。游戏吸引自己的几个原因非常适合训练自己)good

    版权声明 本文首发自微信公共帐号: 学习学习再学习(xiaolai-xuexi) 无需授权即可转载, 甚至无需保留以上版权声明: 转载时请务必注明作者. 以下是<共同成长社区>第 58 次 ...

  5. asp.net下ueditor上传大容量视频报http请求错误的解决方法

    故障现象: 当使用百度编辑器ueditor上传大容量视频或大容量图片的时候,编辑器报“http请求错误”的解决方法详解: 原因分析: 目前很多CMS整合了百度的ueditor编辑器,但是上传稍微大一点 ...

  6. Linux7下配置Nginx站点.

    今天闲来无事,把服务器重新配置了一下,作为开发者,实际上很多人都是长时间不去配置服务器的,所以曾经很多东西又忘掉了差不多. 特在此分享一下配置成功后的配置文件内容. 其实配置后的文件内容很简单,也没有 ...

  7. 使用 acl 编写 UDP 网络程序(UDP 重传及可靠性机制)

    在当今网络世界,虽然大部分网络应用都是基于 TCP 的,但有时 UDP 的网络通信也有用武之处.acl 的网络库中不仅提供了基于 TCP 的网络套接字流,同时也提供了 UDP 的网络库(目前 acl ...

  8. qt中采用宽带speex进行网络语音通话实验程序

    qt中采用宽带speex进行网络语音通话实验程序 本文博客链接:http://blog.csdn.NET/jdh99,作者:jdh,转载请注明.   环境: 主机:WIN8 开发环境:Qt5 3.1. ...

  9. is和==的区别以及编码和解码

    一.is和==的区别 python中对象包含的三个基本要素分别是:id(身份标识),type(数据类型),value(值) is和==都是对对象进行比较判断的,但对对象比较判断的内容不同. ★==是p ...

  10. 这个注解一次搞定限流与熔断降级:@SentinelResource

    在之前的<使用Sentinel实现接口限流>一文中,我们仅依靠引入Spring Cloud Alibaba对Sentinel的整合封装spring-cloud-starter-alibab ...