激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

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

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

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

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

導(dǎo)語

相信大家無論是做前端還是做后端的,都被接口接口文檔所折磨過,前端抱怨接口文檔和后端給的不一致,后端抱怨寫接口文檔很麻煩,所以swagger就誕生了。直接配置即可自動生成接口文檔,而且提供了高效的api測試
話不多說直接開干
導(dǎo)入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可視化界面可分為三個區(qū)域

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

swagger相關(guān)配置

?
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()) //關(guān)于文檔的各種信息
        .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","719801748@qq.com");//個人的聯(lián)系方式
    return new apiinfo("長安的文檔", "長安的開發(fā)文檔", "1.0", "urn:tos",null, "apache 2.0", "http://www.apache.org/licenses/license-2.0", new arraylist());//文檔的各種信息
  }
}

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

?
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用來標(biāo)注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的配置過程

執(zhí)行

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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男女无套免费视频 | 精品国产一区在线 | 亚洲一区二区三区91 | 久久美女色视频 | 3级毛片| 手机免费看一级片 | 日本在线视频一区二区三区 | 国产精品免费一区二区三区四区 | 黄网站在线免费 | 久综合 | 精品二区在线观看 | 91精品国产乱码久久久久久久久 | 国产三级午夜理伦三级 | 视频毛片 | 欧洲黄色一级视频 | 日本欧美一区二区三区视频麻豆 | 大号bbwassbigav头交 | 久久综合九色综合久久久精品综合 | 毛片视频网站 | 在线视频a| 黄色的视频在线观看 | 精品久久久久久综合日本 | 久久久久亚洲视频 | 91美女视频在线观看 | av电影免费在线看 | 欧美精品一区二区三区在线播放 | 国产超碰人人爽人人做人人爱 | 中午字幕无线码一区2020 | 视频一区二区三区在线播放 | 久久久久久久一区二区三区 | 一级电影免费在线观看 | 一及毛片视频 | 欧美性受xxx黑人xyx性爽 | 美女被免费网站在线软件 | 法国性xxx精品hd | videos韩国 | 91久久久久久久一区二区 | 逼特逼视频在线观看 | 欧美男人天堂网 | 成人在线国产 | 免费在线观看国产精品 |