springboot如何整合swagger

小編給大家分享一下springboot如何整合swagger,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

承留網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián),承留網(wǎng)站設(shè)計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為承留成百上千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營銷網(wǎng)站建設(shè)要多少錢,請找那個售后服務(wù)好的承留做網(wǎng)站的公司定做!

springboot整合swagger

簡介

swagger提供最強大,最易用的工具,以充分利用OpenAPI規(guī)范。

官網(wǎng) : https://swagger.io/

準(zhǔn)備工作

  • pom.xml jar引入:  <swagger.version>2.9.2</swagger.version>

     <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>${swagger.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>${swagger.version}</version>
        </dependency>

目的

之前使用swagger都是直接在類,方法,實體上寫api引入的參數(shù),這給人一種代碼很不清爽的感覺,所以采用yaml文件編輯的模式,是代碼看著更簡單。

項目結(jié)構(gòu)

  • 1.創(chuàng)建SwaggerConfig類

package com.honghh.bootfirst.config;

import io.swagger.annotations.ApiOperation;
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;

/**
 * ClassName: SwaggerConfig
 * Description:
 *
 * @author honghh
 * @date 2019/02/20 14:28
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig{

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //加了ApiOperation注解的類,生成接口文檔
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                //包下的類,生成接口文檔
                //.apis(RequestHandlerSelectors.basePackage("com.honghh.bootfirst.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("boot-demo")
                .description("boot-demo文檔")
                .termsOfServiceUrl("http://www.boot-demo.cn")
                .version("1.0.0")
                .build();
    }

}
  • 2.引入swagger展示層代碼,代碼我將上傳到碼云https://gitee.com/honghh/boot-demo.git

  • 3.配置yaml文件

#必要字段!Swagger規(guī)范版本,必須填2.0,否則該YAML將不能用于Swagger其他組件
swagger: '2.0'
#必要字段!描述API接口信息的元數(shù)據(jù)
info:
  #接口文檔的描述
  description: swagger說明文檔,讓一切都變得如此簡單。
  #版本號
  version: 1.0.0
  #接口標(biāo)題
  title: boot-demo
#Swagger會提供測試用例,host指定測試時的主機名,如果沒有指定就是當(dāng)前主機,可以指定端口.
host: localhost:8080
#定義的api的前綴,必須已/開頭,測試用例的主機則為:host+bashPath
#basePath: /boot-demo

#指定調(diào)用接口的協(xié)議,必須是:"http", "https", "ws", "wss".默認(rèn)是http.-表示是個數(shù)組元素,即schemes接受一個數(shù)組參數(shù)
schemes:
  - http
  - https

#對應(yīng)與http協(xié)議頭request的Accept,調(diào)用者可接受類型,默認(rèn)是*/*,定義的類型必須是http協(xié)議定義的 Mime Types,RestfulAPI一般定義成application/json
#這兩個是對所有接口的全局設(shè)置,在細(xì)化的接口中是還可以對應(yīng)這兩個屬性來覆蓋全局屬性
produces:
  - application/json

#定義接口數(shù)據(jù)
paths:
  /myInfo:
    #必要字段!定義HTTP操作方法,必須是http協(xié)議定義的方法
    get:
      tags:
        - MyInfo 用戶信息
      #接口概要
      summary: 用戶信息
      #接口描述
      description: 查詢出所有用戶的所有信息,用戶名,別名
      parameters:
        - name: id
          description: 用戶ID
          in: query
          type: integer
          required: true
      #返回值描述,必要自動
      responses:
        #返回的http狀態(tài)碼
        200:
          description: 所有用戶信息或者用戶的集合信息
          #描述返回值
          schema:
            #返回值格式,可選的有array,integer,string,boolean
            $ref: '#/definitions/myInfo'

#定義數(shù)據(jù)模型
definitions:
  R:
    type: object
    properties:
      code:
        description: 狀態(tài)碼 0:成功  非0:失敗
        type: integer
        format: int32
      msg:
        description: 失敗原因
        type: string
  myInfo:
    type: object
    properties:
      id:
        description: ID
        type: integer
        format: int32
      age:
        description: 年齡
        type: integer
      name:
        description: 姓名
        type: string

4.啟動項目,輸入:http://localhost:8080/swagger/index.html 運行如圖
springboot如何整合swagger
springboot如何整合swagger

看完了這篇文章,相信你對“springboot如何整合swagger”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!

網(wǎng)站欄目:springboot如何整合swagger
鏈接URL:http://muchs.cn/article32/ihsepc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、Google、品牌網(wǎng)站制作、網(wǎng)站導(dǎo)航、做網(wǎng)站網(wǎng)站設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計公司