基于SpringCloud的Microservices架构实战案例-在线API管理

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.  

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

  9. <dependency>

  10.    <groupId>io.springfox</groupId>

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

  12.    <version>2.6.1</version>

  13. </dependency>

第二步编写配置管理类Swagger2Config

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

  2.  

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

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

  5.  

  6. import io.swagger.annotations.ApiOperation;

  7. import springfox.documentation.builders.ApiInfoBuilder;

  8. import springfox.documentation.builders.PathSelectors;

  9. import springfox.documentation.builders.RequestHandlerSelectors;

  10. import springfox.documentation.service.ApiInfo;

  11. import springfox.documentation.spi.DocumentationType;

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

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

  14.  

  15. /**

  16. * swagger2 configuration

  17. *

  18. * @author guooo

  19. *

  20. */

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

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

  23. public class Swagger2Config {

  24.  

  25.    @Bean

  26.    public Docket createRestApi() {

  27.        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()

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

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

  30.                .paths(PathSelectors.any())

  31.                .build();

  32.    }

  33.  

  34.    private ApiInfo apiInfo() {

  35.        return new ApiInfoBuilder().title("Front app Swagger apis").description("For micro-service 's app to use")

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

  37.    }

  38. }

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

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

  2. @RestController

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

  4. public class APIAccountController {

  5.  

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

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

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

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

  10.        RestAPIResult<String> restAPIResult = new RestAPIResult<>();

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

  12.    }

使用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. public class Bootstrap {

  4.  

  5.    public static void main(String[] args) {

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

  7.    }

  8.  

  9. }

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

参数配置,配置示例

 
  1. swagger.enabled=true

  2.  

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

  4. swagger.description=Starter for swagger 2.x

  5. swagger.version=1.4.0.RELEASE

  6. swagger.license=Apache License, Version 2.0

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

  8.  

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

  10.  

  11. swagger.contact.name=didi

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

  13.  

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

  15. swagger.base-package=com.didispace

  16. swagger.base-path=/**

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

  18.  

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

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

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

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

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

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

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

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

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

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

  29.  

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

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

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

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

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

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

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

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

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

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

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

 

扩展阅读:

 

欢迎加入我的星球

 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MavenTalk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值