SpringBoot集成swagger的實(shí)例代碼
Swagger 是一款RESTFUL接口的文檔在線自動(dòng)生成+功能測試功能軟件。本文簡單介紹了在項(xiàng)目中集成swagger的方法和一些常見問題。如果想深入分析項(xiàng)目源碼,了解更多內(nèi)容,見參考資料。
Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)??傮w目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來更新。文件的方法,參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來始終保持同步。Swagger 讓部署管理和使用功能強(qiáng)大的API從未如此簡單。
對于搬磚的同學(xué)來說,寫接口容易,寫接口文檔很煩,接口變動(dòng),維護(hù)接口文檔就更更更煩,所以經(jīng)常能發(fā)現(xiàn)文檔與程序不匹配。
等過一段時(shí)間就連開發(fā)者也蒙圈了
Swagger2快速方便的解決了以上問題。一個(gè)能與Spring MVC程序配合組織出強(qiáng)大RESTful API文檔的新寵兒。
下面直接上代碼
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zhongxin.wealth</groupId>
<artifactId>wealthweb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>wealthweb</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
</dependencies>
</project>創(chuàng)建配置類
package com.zhongxin.wealth.apiConfig;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
/**
* Created by DingYS on 2017/12/8.
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.zhongxin.wealth.web"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("廊坊委貸大數(shù)據(jù)統(tǒng)計(jì)結(jié)果輸出接口")
.version("1.0")
.build();
}
}controller編寫
package com.zhongxin.wealth.web;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by DingYS on 2017/12/7.
*/
@RestController
@RequestMapping("/hello")
public class HelloWordController {
@ApiOperation(value="測試接口", notes="這只是一個(gè)測試controller調(diào)用的接口,沒有任何的業(yè)務(wù)邏輯")
@RequestMapping(value = {"/test"},method = RequestMethod.GET)
public String testHello(){
return "hello";
}
}代碼完成,準(zhǔn)備看效果

點(diǎn)擊Try it out!

是不是很詳細(xì),很高大上。
注:集成過程中剛開始用的swagger2.2.2版本,會在首頁出現(xiàn)一個(gè)error的錯(cuò)誤提醒
{“schemaValidationMessages”:[{“level”:”error”,”message”:”Can’t read from file http://127.0.0.1:8888/v2/api-docs"}]}
但是瀏覽器訪問:http://127.0.0.1:8888/v2/api-docs 又能獲取 結(jié)果
{“swagger”:”2.0”,”info”:{“version”:”1.0”,”title”:”廊坊委貸大數(shù)據(jù)統(tǒng)計(jì)結(jié)果輸出接口”,”contact”:{},”license”:{}},”host”:”127.0.0.1:8888”,”basePath”:”/“,”tags”:[{“name”:”hello-word-controller”,”description”:”Hello Word Controller”}],”paths”:{“/hello/test”:{“get”:{“tags”:[“hello-word-controller”],”summary”:”測試接口”,”description”:”這只是一個(gè)測試controller調(diào)用的接口,沒有任何的業(yè)務(wù)邏輯”,”operationId”:”testHelloUsingGET”,”consumes”:[“application/json”],”produces”:[“/“],”responses”:{“200”:{“description”:”OK”,”schema”:{“type”:”string”}},”401”:{“description”:”Unauthorized”},”403”:{“description”:”Forbidden”},”404”:{“description”:”Not Found”}}}}}}
具體原因本人不明,換成2.7.0版本以后沒在出現(xiàn)。
總結(jié)
以上所述是小編給大家介紹的SpringBoot集成swagger的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
dubbo服務(wù)引用之創(chuàng)建Invoker流程詳解
這篇文章主要為大家介紹了dubbo服務(wù)引用二之創(chuàng)建Invoker流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08
SpringMVC中使用Thymeleaf模板引擎實(shí)例代碼
這篇文章主要介紹了SpringMVC中使用Thymeleaf模板引擎實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02
Java數(shù)據(jù)結(jié)構(gòu)之圖(動(dòng)力節(jié)點(diǎn)Java學(xué)院整理)
本文章主要講解學(xué)習(xí)如何使用JAVA語言以鄰接表的方式實(shí)現(xiàn)了數(shù)據(jù)結(jié)構(gòu)---圖(Graph)。對java數(shù)據(jù)結(jié)構(gòu)之圖相關(guān)知識感興趣的朋友一起學(xué)習(xí)吧2017-04-04
Spring實(shí)現(xiàn)Logback日志模板設(shè)置動(dòng)態(tài)參數(shù)功能
本文介紹如何在Spring應(yīng)用中實(shí)現(xiàn)Logback日志模板的動(dòng)態(tài)參數(shù)設(shè)置,通過設(shè)計(jì)traceId鏈路標(biāo)識,將其動(dòng)態(tài)添加到日志輸出中,從而實(shí)現(xiàn)簡易的鏈路追蹤功能,感興趣的朋友跟隨小編一起看看吧2024-11-11
Mybatis中3種關(guān)聯(lián)關(guān)系的實(shí)現(xiàn)方法示例
這篇文章主要給大家介紹了關(guān)于Mybatis中3種關(guān)聯(lián)關(guān)系的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用Mybatis具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
spring cloud oauth2 feign 遇到的坑及解決
這篇文章主要介紹了spring cloud oauth2 feign 遇到的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java并發(fā)之條件阻塞Condition的應(yīng)用代碼示例
這篇文章主要介紹了Java并發(fā)之條件阻塞Condition的應(yīng)用代碼示例,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02

