simplemall項目前幾篇回顧:html
源碼地址:https://github.com/backkoms/simplemallgithub
前端和後端的惟一聯繫,變成了API接口;API文檔變成了先後端開發人員聯繫的紐帶,變得愈來愈重要,swagger就是一款讓你更好的書寫API文檔的框架。 本實戰案例中也引入swagger2做爲API管理工具,下面羅列下swagger2+SpringBoot使用步驟。web
SpringBoot集成Swagger2
第一步,pom配置spring
-
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
apache -
<dependency>
後端 -
<groupId>io.springfox</groupId>
api -
<artifactId>springfox-swagger2</artifactId>
-
<version>2.6.1</version>
-
</dependency>
-
-
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
-
<dependency>
-
<groupId>io.springfox</groupId>
-
<artifactId>springfox-swagger-ui</artifactId>
-
<version>2.6.1</version>
-
</dependency>
第二步編寫配置管理類Swagger2Config
-
package com.simplemall.micro.serv.page;
-
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
-
import io.swagger.annotations.ApiOperation;
-
import springfox.documentation.builders.ApiInfoBuilder;
-
import springfox.documentation.builders.PathSelectors;
-
import springfox.documentation.builders.RequestHandlerSelectors;
-
import springfox.documentation.service.ApiInfo;
-
import springfox.documentation.spi.DocumentationType;
-
import springfox.documentation.spring.web.plugins.Docket;
-
import springfox.documentation.swagger2.annotations.EnableSwagger2;
-
-
/**
-
* swagger2 configuration
-
*
-
* @author guooo
-
*
-
*/
-
@Configuration//SpringBoot啓動時自動裝載
-
@EnableSwagger2//打開swagger2功能,缺失的話一樣沒法打開ui頁面
-
publicclassSwagger2Config{
-
-
@Bean
-
publicDocket createRestApi(){
-
returnnewDocket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
-
.apis(RequestHandlerSelectors.basePackage("com.simplemall.micro.serv.page.api"))
-
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
-
.paths(PathSelectors.any())
-
.build();
-
}
-
-
privateApiInfo apiInfo(){
-
returnnewApiInfoBuilder().title("Front app Swagger apis").description("For micro-service 's app to use")
-
.version("V1.0").build();
-
}
-
}
通過以上兩步簡單的配置後,能夠直接進行接口代碼的編寫。
-
@Api(value ="用戶服務", tags ="用戶服務接口")
-
@RestController
-
@RefreshScope// 使用該註解的類,會在接到SpringCloud配置中心配置刷新的時候,自動將新的配置更新到該類對應的字段中。須要從新觸發加載動做可使用POST方式請求/refresh接口,該接口位於spring-boot-starter-actuator依賴,調用前需添加不然404。
-
publicclassAPIAccountController{
-
-
@ApiOperation(value ="用戶登錄")
-
@RequestMapping(value ="acc/login", method ={RequestMethod.POST })
-
publicRestAPIResult<String> login(@ApiParam(value ="手機號")@RequestParam(required =true)String phone,
-
@ApiParam(value ="密碼")@RequestParam(required =true)String password,HttpSession session){
-
RestAPIResult<String> restAPIResult =newRestAPIResult<>();
-
Account account = accountFeignClient.login(phone, password);
-
}
使用swagger進行API管理的話,對代碼有必定的侵入性,這個須要考慮在內。以前也提到過幾種在線API的管理方式,點擊連接《介紹幾款經常使用的在線API管理工具》
使用SpringBoot技術,再以maven原始的方式引入swagger使用的話,遠不如一個starter來的爽,這裏介紹一個swagger-starter,能夠更快捷的與spring boot集成使用。
swagger-spring-boot-starter應用
在pom.xml中引入依賴:【當前最新版本 1.7.0.RELEASE】
-
<dependency>
-
<groupId>com.spring4all</groupId>
-
<artifactId>swagger-spring-boot-starter</artifactId>
-
<version>1.7.0.RELEASE</version>
-
</dependency>
注意:從1.6.0開始,咱們按Spring Boot官方建議修改了artifactId爲swagger-spring-boot-starter,1.6.0以前的版本不作修改,依然爲使用spring-boot-starter-swagger !
在應用主類中增長@EnableSwagger2Doc註解
-
@EnableSwagger2Doc
-
@SpringBootApplication
-
publicclassBootstrap{
-
-
publicstaticvoid main(String[] args){
-
SpringApplication.run(Bootstrap.class, args);
-
}
-
-
}
默認狀況下就能產生全部當前Spring MVC加載的請求映射文檔。
參數配置,配置示例
-
swagger.enabled=true
-
-
swagger.title=spring-boot-starter-swagger
-
swagger.description=Starterfor swagger 2.x
-
swagger.version=1.4.0.RELEASE
-
swagger.license=ApacheLicense,Version2.0
-
swagger.licenseUrl=https://www.apache.org/licenses/LICENSE-2.0.html
-
-
swagger.termsOfServiceUrl=https://github.com/dyc87112/spring-boot-starter-swagger
-
-
swagger.contact.name=didi
-
swagger.contact.url=http://blog.didispace.com
-
-
swagger.contact.email=dyc87112@qq.com
-
swagger.base-package=com.didispace
-
swagger.base-path=/**
-
swagger.exclude-path=/error, /ops/**
-
-
swagger.globalOperationParameters[0].name=name one
-
swagger.globalOperationParameters[0].description=some description one
-
swagger.globalOperationParameters[0].modelRef=string
-
swagger.globalOperationParameters[0].parameterType=header
-
swagger.globalOperationParameters[0].required=true
-
swagger.globalOperationParameters[1].name=name two
-
swagger.globalOperationParameters[1].description=some description two
-
swagger.globalOperationParameters[1].modelRef=string
-
swagger.globalOperationParameters[1].parameterType=body
-
swagger.globalOperationParameters[1].required=false
-
-
// 取消使用默認預約義的響應消息,並使用自定義響應消息
-
swagger.apply-default-response-messages=false
-
swagger.global-response-message.get[0].code=401
-
swagger.global-response-message.get[0].message=401get
-
swagger.global-response-message.get[1].code=500
-
swagger.global-response-message.get[1].message=500get
-
swagger.global-response-message.get[1].modelRef=ERROR
-
swagger.global-response-message.post[0].code=500
-
swagger.global-response-message.post[0].message=500post
-
swagger.global-response-message.post[0].modelRef=ERROR
詳細介紹可參考源碼,地址:https://github.com/SpringForAll/spring-boot-starter-swagger。因爲JDK代碼編譯版本的限制,JDK1.7是不支持的,可以使用1.8
擴展閱讀:
