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. 关于Socket的建立及连接

    最近在做一个东西,目前考虑的是采用Socket连接,从网上找了一个SuperSocket的socket通信框架.... 关于Socket连接,个人并不是搞得太懂,写的东西也全是个人理解,那么这里先整理 ...

  2. WPF获取和设置应用程序范围的资源

    设置资源: Application.Current.Resources["ApplicationScopeResource"] = Brushes.White; 使用代码获取资源: ...

  3. 微信小程序把玩(二十六)navigator组件

    原文:微信小程序把玩(二十六)navigator组件 navigator跳转分为两个状态一种是关闭当前页面一种是不关闭当前页面.用redirect属性指定. 主要属性: wxml <naviga ...

  4. 什么是BSON?

    BSON( Binary Serialized Document Format) 是一种二进制形式的存储格式,采用了类似于 C 语言结构体的名称.对表示方法,支持内嵌的文档对象和数组对象,具有轻量性. ...

  5. hadoop(三)

    hadoop(三) 1.对MapReduce的认识   MapReduce是运行在yarn上面的一个分布式运算框架,它是用来解决海量的分布式运算的.对于MapReduce来说,我们可以把它分成两部分来 ...

  6. Qt5.5.0在Linux下静态编译(加上-fontconfig编译项才能显示中文) good

    测试系统环境:Ubuntu12.04 (32bit/64bit)编译软件环境:QT5.5.0   本文章主要介绍Linux下QT静态编译环境的搭建,以及如何编译我们的程序board_driver. 1 ...

  7. 2015新款 MacBook 用心的测评与试用. 最轻薄的Mac上市

    2015新款 MacBook 不一样的测评与试用. 最轻薄的Mac上市了. 直击现场 <HTML开发MacOSApp教程>  http://pan.baidu.com/s/1jG1Q58M ...

  8. QTableWidget和 QTableView翻页效果(准确计算Scroll,然后使用setSliderPosition函数)

    以QTableView举例,QTableWidget使用相同   int CQTTableViewPageSwitch::pageCount(QTableView *p)//QTableView 总页 ...

  9. Qt 访问网络的 HttpClient(封装QNetworkAccessManager,且有服务端)

    Qt 使用 QNetworkAccessManager 访问网络,这里对其进行了简单的封装,访问网络的代码可以简化为: 1 2 3 HttpClient("http://localhost: ...

  10. VS Code真机测试步骤

    VS Code真机测试步骤 前提:你的电脑跟你的手机是在同一个网络环境下.电脑连手机热点: 1. 在扩展里搜索live server,下载安装: 2. 打开cmd 命令窗口(快捷键是win+r): 输 ...