一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - SpringBoot集成swagger的實例代碼

SpringBoot集成swagger的實例代碼

2021-03-02 10:38Smilence^^ Java教程

Swagger 是一款RESTFUL接口的文檔在線自動生成+功能測試功能軟件。這篇文章主要介紹了SpringBoot集成swagger,需要的朋友可以參考下

swagger 是一款restful接口的文檔在線自動生成+功能測試功能軟件。本文簡單介紹了在項目中集成swagger的方法和一些常見問題。如果想深入分析項目源碼,了解更多內容,見參考資料。

swagger 是一個規(guī)范和完整的框架,用于生成、描述、調用和可視化 restful 風格的 web 服務。總體目標是使客戶端和文件系統(tǒng)作為服務器以同樣的速度來更新。文件的方法,參數(shù)和模型緊密集成到服務器端的代碼,允許api來始終保持同步。swagger 讓部署管理和使用功能強大的api從未如此簡單。

對于搬磚的同學來說,寫接口容易,寫接口文檔很煩,接口變動,維護接口文檔就更更更煩,所以經(jīng)常能發(fā)現(xiàn)文檔與程序不匹配。

等過一段時間就連開發(fā)者也蒙圈了

swagger2快速方便的解決了以上問題。一個能與spring mvc程序配合組織出強大restful api文檔的新寵兒。

下面直接上代碼

pom.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?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)建配置類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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)計結果輸出接口")
        .version("1.0")
        .build();
  }
}

  controller編寫

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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="這只是一個測試controller調用的接口,沒有任何的業(yè)務邏輯")
  @requestmapping(value = {"/test"},method = requestmethod.get)
  public string testhello(){
    return "hello";
  }
}

  代碼完成,準備看效果

SpringBoot集成swagger的實例代碼

點擊try it out!

SpringBoot集成swagger的實例代碼

是不是很詳細,很高大上。

注:集成過程中剛開始用的swagger2.2.2版本,會在首頁出現(xiàn)一個error的錯誤提醒

?
1
{“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  又能獲取 結果

?
1
{“swagger”:”2.0”,”info”:{“version”:”1.0”,”title”:”廊坊委貸大數(shù)據(jù)統(tǒng)計結果輸出接口”,”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”:”這只是一個測試controller調用的接口,沒有任何的業(yè)務邏輯”,”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)。

總結

以上所述是小編給大家介紹的springboot集成swagger的實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!

原文鏈接:http://www.cnblogs.com/Smilence1024/p/8004814.html?utm_source=tuicool&utm_medium=referral

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美亚洲视频在线观看 | 免费看片黄 | 亚洲乱码一区二区三区国产精品 | 小舞同人18av黄漫网站 | 国产精品久久久久久久午夜片 | 亚洲欧美在线免费观看 | 欧美日韩一区二区综合在线视频 | 无限观看社区在线视频 | 成功精品影院 | 99热这里有精品 | 18韩国美女vip视频7 | 韩国三级理韩国三级理人伦 | boobsmilking流奶水野战 | 手机在线免费观看视频 | 青草视频在线观看免费视频 | 亚洲国产欧美在线人网站 | 香蕉国产成版人视频在线观看 | 国产色视频网站 | 四虎影院最新网址 | 亚洲卡一卡2卡三卡4麻豆 | 啪一啪日一日 | 白丝校花好湿好紧 | 国产精品亚洲片夜色在线 | 欧美人禽杂交狂配无删完整 | 2021最新国产成人精品视频 | 暖暖视频免费观看视频中国.韩剧 | 欧美一级视频在线 | 91李宗精品72集在线观看 | 国产嘿咻 | 顶级尤物极品女神福利视频 | 久久re视频这里精品一本到99 | 天天综合天天综合 | yjsp妖精视频在线观看免费 | 国产精品露脸国语对白河北 | 51精品 | 成人精品亚洲 | 成人免费视频一区二区三区 | 久久99re2热在线播放7 | 欧美成a人片免费看久久 | 美女在线看永久免费网址 | 奇米影视奇米色777欧美 |