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. Android Camera2 拍照(四)——对焦模式

    原文:Android Camera2 拍照(四)--对焦模式 本篇将重点介绍使用Camera2 API进行手动对焦的设置,以及在手动对焦与自动对焦模式之间切换. 一.手动对焦响应事件 首先我们要实现点 ...

  2. 使用Newtonsoft.Json输出JSON

    安装: Install-Package Newtonsoft.Json 代码: //序列化DataTable DataTable dt = new DataTable(); dt.Columns.Ad ...

  3. A simple in-process HTTP server for UWP

    原文 http://www.dzhang.com/blog/2012/09/18/a-simple-in-process-http-server-for-windows-8-metro-apps 简单 ...

  4. MinGW 编译 libsndfile-1.0.25(只要有 MSYS,./configure make make install 就行了)

    最近做的一个项目需要读写 wav 文件.在网上找到 libsndfile 刚好满足我的需要.但是编译的时候遇到了点小麻烦,这里记录一下编译的过程,免得下次再编译时忘记了. 因为是在编译完成若干天后写的 ...

  5. QML Settings 小的示例

    QML 中使用 Settings 可以保存一些简单的信息,例如用户名,密码,窗口位置,大小等,没有Sqlite那么麻烦,简单易用哦~~~(环境:Qt5.8  for android ,Windows ...

  6. <%@ Application Codebehind="Global.asax.cs" Inherits="XXX.MvcApplication" Language="C#" %>

    <%@ Application Codebehind="Global.asax.cs" Inherits="XXX.MvcApplication" Lan ...

  7. 《HTML开发Mac OS App 视频教程》 第001讲、入门教程

    土豆网同步更新:http://www.tudou.com/plcover/VHNh6ZopQ4E/   使用HTML 创建Mac OS App 视频教程. 官方QQ群: (1)App实践出真知 434 ...

  8. Building PySide on Microsoft Windows

    Prerequisites MS Visual Studio Express 2008 [microsoft.com] NOTE: Visual Studio Express 2010 is not ...

  9. Qt5---ftp上传功能(使用组合的办法实现功能,QNetworkAccessManager自动管理分片上传,用QLoggingCategory屏蔽SSL警告)

      从Qt的版本进化中可以知道,在Qt4中的QFtp类到了Qt5中已经没有了,虽然可以通过在Qt5中自己编译出QFtp,但是Qt5中提供的QNetworkAccessManager在发送和请求网络方面 ...

  10. spring通过注解方式依赖注入原理 (私有成员属性如何注入)

    一.spring如何创建依赖的对象 用过spring的都知道我们在dao.service层加上@repository.@Service就能将这两个对象交给spring管理,在下次使用的时候使用@res ...