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

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

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

服務器之家 - 編程語言 - Java教程 - 手把手教你SpringBoot快速集成Swagger的配置過程

手把手教你SpringBoot快速集成Swagger的配置過程

2021-08-11 11:43常安、 Java教程

這篇文章主要介紹了手把手教你SpringBoot快速集成Swagger的配置過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

導語

相信大家無論是做前端還是做后端的,都被接口接口文檔所折磨過,前端抱怨接口文檔和后端給的不一致,后端抱怨寫接口文檔很麻煩,所以swagger就誕生了。直接配置即可自動生成接口文檔,而且提供了高效的api測試
話不多說直接開干
導入springboot集成swagger所需要的依賴

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--web方便測試-->
   <dependency>
     <groupid>org.springframework.boot</groupid>
     <artifactid>spring-boot-starter-web</artifactid>
   </dependency>
   <!-- swagger2核心包 -->
   <dependency>
     <groupid>io.springfox</groupid>
     <artifactid>springfox-swagger2</artifactid>
     <version>2.9.2</version>
   </dependency>
   <!-- swagger-ui 可視化界面 -->
   <dependency>
     <groupid>io.springfox</groupid>
     <artifactid>springfox-swagger-ui</artifactid>
     <version>2.9.2</version>
   </dependency>

swagger可視化界面可分為三個區域

手把手教你SpringBoot快速集成Swagger的配置過程

swagger相關配置

?
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
package com.example.config;
 
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import springfox.documentation.builders.requesthandlerselectors;
import springfox.documentation.service.apiinfo;
import springfox.documentation.service.contact;
import springfox.documentation.spi.documentationtype;
import springfox.documentation.spring.web.plugins.docket;
import springfox.documentation.swagger2.annotations.enableswagger2;
 
import java.util.arraylist;
 
@configuration
@enableswagger2 //開啟swagger的使用
public class swaggerconfig {
 
  @bean  //swagger的使用主要是要將docket對象傳入ioc容器
  public docket docket(){
    return new docket(documentationtype.swagger_2)
        .apiinfo(apiinfo()) //關于文檔的各種信息
        .enable(true) //使swagger生效
        .groupname("常安祖")
        .select()//選擇掃描的接口
        .apis(requesthandlerselectors.basepackage("com.example.controller"))//指定掃描的接口
        .build();
  }
 
 
  public apiinfo apiinfo(){
    contact contact = new contact("長安","https://blog.csdn.net/weixin_45647685","[email protected]");//個人的聯系方式
    return new apiinfo("長安的文檔", "長安的開發文檔", "1.0", "urn:tos",null, "apache 2.0", "http://www.apache.org/licenses/license-2.0", new arraylist());//文檔的各種信息
  }
}

@apimodel( ) //主要用來標注返回的實體類
@apimodelproperty( ) //主要用來標注實體類中的屬性
案例:

?
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
@apimodel("用戶的實體類")
public class user implements serializable {
 
  @apimodelproperty("用戶的id")
  private integer id;
 
  @apimodelproperty("用戶的姓名")
  private string name;
 
  @apimodelproperty("用戶的年紀")
  private integer age;
 
  public integer getid() {
    return id;
  }
  public user(integer id, string name, integer age) {
    this.id = id;
    this.name = name;
    this.age = age;
  }
 
  public void setid(integer id) {
    this.id = id;
  }
 
  public string getname() {
    return name;
  }
 
  public void setname(string name) {
    this.name = name;
  }
 
  public integer getage() {
    return age;
  }
 
  public void setage(integer age) {
    this.age = age;
  }
}

@apimodelproperty用來標注api接口
案例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.yangzihao.controller;
 
import com.yangzihao.entity.user;
import io.swagger.annotations.apimodelproperty;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.requestmapping;
 
@restcontroller
public class usercontroller {
 
  @apimodelproperty("得到一個user")
  @getmapping("/getuser")
  public user getuser(){
    return new user(1,"測試",18);
  }
}

進入swagger可視化界面

手把手教你SpringBoot快速集成Swagger的配置過程

使用swagger進行接口測試

手把手教你SpringBoot快速集成Swagger的配置過程

執行

手把手教你SpringBoot快速集成Swagger的配置過程

到此這篇關于手把手教你springboot快速集成swagger的配置過程的文章就介紹到這了,更多相關springboot集成swagger內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/weixin_45647685/article/details/113850616

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美久久天天综合香蕉伊 | 亚洲va韩国va欧美va天堂 | 日本护士撒尿 | 国产精品男人的天堂 | 精品免费视在线视频观看 | 男人操美女视频 | 白丝萝莉h | 九九九九九九伊人 | 91精品国产99久久 | 精品国产三级av在线 | 美女露尿口| 91麻豆国产 | 日本高清在线不卡 | 国自产拍在线天天更新91 | 亚洲色图首页 | 欧美专区在线播放 | 国产一级在线观看视频 | 秋霞黄色大片 | 四虎永久免费在线观看 | 亚洲 日韩经典 中文字幕 | 色综合亚洲精品激情狠狠 | 国产乱码一卡二卡3卡四卡 国产乱插 | 亚洲AV无码乱码国产麻豆穿越 | 韩国美女豪爽一级毛片 | 亚洲国产精品综合欧美 | 国产成人精品三级在线 | 国产欧美精品 | 女人被爽到呻吟娇喘的视频动态图 | 国产欧美日韩不卡一区二区三区 | 91在线精品国产 | 拔插拔插.com | 色人阁导航 | 无码中文字幕热热久久 | 亚洲精品丝袜在线一区波多野结衣 | 日本艳鉧动漫1~6在线观看 | 亚洲精品在线看 | yy6080欧美三级理论 | 草馏社区最新1024 | 欧美精品久久久久久久影视 | 国产香蕉一区二区在线观看 | 韩国最新三级网站在线播放 |