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. 字符串、数组操作函数 Copy Concat Delete Insert High MidStr Pos SetLength StrPCopy TrimLeft

    对字符串及数组的操作,是每个程序员必须要掌握的.熟练的使用这些函数,在编程时能更加得心应手. 1.Copy 功能说明:该函数用于从字符串中复制指定范围中的字符.该函数有3个参数.第一个参数是数据源(即 ...

  2. linux 用蓝牙和手机通信

    加载模块: # modprobe hci_usb    # modprobe rfcomm    # hciconfig hci0 up # hciconfig hci0 up 查看状况: # hci ...

  3. winform实现DataGridView全选

    之前写过,一时想不起来就在网上找了一些.结果感觉好麻烦.于是就自己打开之前做过的功能.找到源码. private void CheckDatabasexuan() { DataGridViewChec ...

  4. java设计模式(2)

    工厂模式定义 在面向对象程序设计中,工厂通常是用来创建其他对象的对象,工产模式根据不同的参数来实现不同的分配方案和创建对象. 在工产模式中,我们创建对象时不会对客户端暴露创建逻辑,而且是通过使用一个共 ...

  5. WPF Textblock Run 空白问题

    消除Run之前的空白是将Run标签布局时头尾相连 如: <TextBlock > <Run Text="A"></Run> <Run Te ...

  6. VS2008发布项目“发布失败”,没有提示错误

    VS2008发布项目时发布失败,但是没有提示任何的错误. 解决方法: 组合键”Ctrl+Alt+O”; 根据这个我知道了,是因为我更改了文件名,发布时找不到导致的,然后在资源管理器中找到那一项,删除或 ...

  7. SMC状态机笔记

    %class 状态机所作用的类 %package 类所在的包 %fsmclass 生成类的类名 %fsmfile 生成类的文件名 %access 生成类的可访问级别 %start 指定状态机的开始状态 ...

  8. HTTP RFC7230

    https://tools.ietf.org/html/rfc7230 https://tools.ietf.org/html/rfc7231#section-4

  9. 静态编译 Qt 5.6

    源码下载 Qt git地址:git://code.qt.io/qt/qt5.git 推荐用 SourceTree 客户端来克隆源码 准备工作 系统 Windows 7 或者 Windows 10 编译 ...

  10. D7下FastMM的使用

    原文出处:http://hi.baidu.com/showwindows/blog/item/5b7ac601c487c605728da573.html FastMM 快速MM:-),在D2006和2 ...