溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何在SpringBoot中利用Swagger2構(gòu)建一個API文檔

發(fā)布時間:2020-12-18 14:26:33 來源:億速云 閱讀:157 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)如何在SpringBoot中利用Swagger2構(gòu)建一個API文檔,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

第一部分:代碼集成

pom.xml

<!--swagger2配置-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.4.0</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.4.0</version>
    </dependency>
    <dependency>
      <groupId>com.github.xiaoymin</groupId>
      <artifactId>swagger-bootstrap-ui</artifactId>
      <version>1.6</version>
    </dependency>

swagger2配置類

package com.liud.demo.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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * TODO
 * swagger2配置類
 * @author liud
 * @version 1.0
 */

@Configuration
@EnableSwagger2
public class Swagger2 {
  //配置swagger2核心配置
  @Bean
  public Docket createRestApi(){
    return new Docket(DocumentationType.SWAGGER_2) //指定api類型位swagger2
        .apiInfo(apiInfo())            //用于定義api文檔匯總信息
        .select()
        //.apis(RequestHandlerSelectors.basePackage("com.liud.demo.controller")) //指定生成文檔的controller
        //.apis(RequestHandlerSelectors.any()) //為任何接口生成API文檔
        //.apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //為有@Api注解的Controller生成API文檔
        .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) //為有@ApiOperation注解的方法生成API文檔
        .paths(PathSelectors.any())
        .build();
  }

  //api基本信息
  private ApiInfo apiInfo(){
    return new ApiInfoBuilder()
        .title("SpringBootDemo的項目接口API") //文檔標(biāo)題
        .contact(new Contact("liud", //作者
            "",
            "")) //聯(lián)系人
        .description("SpringBootDemo的項目接口API")//詳細(xì)信息
        .version("1.0.0")//文檔版本號
        .termsOfServiceUrl("")//網(wǎng)站地址
        .build();
  }
}

Controller

package com.liud.demo.controller;

import com.liud.demo.service.HelloService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * TODO
 *
 * @author liud
 * @version 1.0
 */
@RestController
@Api(tags = {"hello操作接口"})
public class HelloController {
  @ApiOperation(value = "根據(jù)用戶名獲取用戶信息接口")
  @RequestMapping(value = "/getuserinfo",method = RequestMethod.POST)
  public String getUserInfo(HttpServletRequest request,
               @ApiParam(name="username",value = "用戶名",required = true) String username){
    return "輸入的姓名:"+username+",這個用戶的信息已經(jīng)存在!";
  }
}

第二部分 使用 ①原路徑模式

在瀏覽器上輸入url:
http://{ip}:{port}/swagger-ui.html#/

我的地址:http://127.0.0.1:8081/swagger-ui.html

如何在SpringBoot中利用Swagger2構(gòu)建一個API文檔

②文檔模式

在瀏覽器上輸入url:
http://{ip}:{port}/doc.html

我的地址:http://127.0.0.1:8081/doc.html

如何在SpringBoot中利用Swagger2構(gòu)建一個API文檔

第三部分 swagger2常用注解

常用注解:

@Api()用于類;
表示標(biāo)識這個類是swagger的資源
tags–表示說明
value–也是說明,可以使用tags替代
但是tags如果有多個值,會生成多個list

如何在SpringBoot中利用Swagger2構(gòu)建一個API文檔

效果:

如何在SpringBoot中利用Swagger2構(gòu)建一個API文檔

@ApiOperation()用于方法;
表示一個http請求的操作
value用于方法描述
notes用于提示內(nèi)容
tags可以重新分組(視情況而用)

@ApiParam()用于方法,參數(shù),字段說明;
表示對參數(shù)的添加元數(shù)據(jù)(說明或是否必填等)
name–參數(shù)名
value–參數(shù)說明
required–是否必填

@ApiParam(name="username",value = "用戶名",required = true) String username

效果:

如何在SpringBoot中利用Swagger2構(gòu)建一個API文檔

  • @ApiModel()用于類

  • 表示對類進行說明,用于參數(shù)用實體類接收

  • @ApiModelProperty()用于方法,字段

  • 表示對model屬性的說明或者數(shù)據(jù)操作更改

  • @ApiIgnore()用于類,方法,方法參數(shù)

  • 表示這個方法或者類被忽略

  • @ApiImplicitParam() 用于方法

  • 表示單獨的請求參數(shù)

  • @ApiImplicitParams() 用于方法,包含多個@ApiImplicitParam

關(guān)于如何在SpringBoot中利用Swagger2構(gòu)建一個API文檔就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI