SpringBoot整合Swagger框架的方法

這篇文章主要講解了SpringBoot整合Swagger框架的方法,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

創(chuàng)新互聯(lián)建站專注于東坡網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供東坡?tīng)I(yíng)銷(xiāo)型網(wǎng)站建設(shè),東坡網(wǎng)站制作、東坡網(wǎng)頁(yè)設(shè)計(jì)、東坡網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開(kāi)發(fā)服務(wù),打造東坡網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供東坡網(wǎng)站排名全網(wǎng)營(yíng)銷(xiāo)落地服務(wù)。

Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)。

總體目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來(lái)更新。文件的方法、參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許 API 來(lái)始終保持同步。Swagger 讓部署管理和使用功能強(qiáng)大的 API 從未如此簡(jiǎn)單。

引入maven依賴

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.9.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.9.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.10</version>
      <scope>provided</scope>
    </dependency>

創(chuàng)建配置類(lèi)

package com.example.demo.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;

/**
 * @author yvioo。
 */
@Configuration
@EnableSwagger2 //開(kāi)啟Swagger2
public class SwaggerConfig {


  /**
   * 配置Swagger的Docket的bean實(shí)例
   * @return
   */
  @Bean
  public Docket docket(Environment environment) {

    //設(shè)置只在開(kāi)發(fā)中環(huán)境中啟動(dòng)swagger
    Profiles profiles=Profiles.of("dev");

    //表示如果現(xiàn)在是dev環(huán)境,則返回true 開(kāi)啟swagger
    boolean flag=environment.acceptsProfiles(profiles);



    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        //是否啟動(dòng)swagger 默認(rèn)啟動(dòng)
        .enable(flag)
        //所在分組
        .groupName("yvioo")
        .select()
        //指定掃描的包路徑
        .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
        //指定掃描的請(qǐng)求,這里表示掃描 /hello/ 的請(qǐng)求
        //.paths(PathSelectors.ant("/hello/**"))
        .build();
  }


  /**
   * 配置ApiInfo信息
   * @return
   */
  private ApiInfo apiInfo() {

    //作者信息
    Contact author = new Contact("yvioo", "https://www.cnblogs.com/pxblog/", "111@qq.com");


    return new ApiInfo(
        "Swagger測(cè)試",
        "Swagger描述",
        "1.0",
        "urn:tos",
        author,
        "Apache 2.0",
        "http://www.apache.org/licenses/LICENSE-2.0",
        new ArrayList()
    );

  }
}

測(cè)試用戶實(shí)體類(lèi)

User.java

package com.example.demo.entity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel("用戶實(shí)體類(lèi) User") //增加實(shí)體類(lèi)接口注釋
@Data  //使用Lombok插件自動(dòng)生成get set方法,這樣才能在swagger中顯示屬性值
public class User {
  @ApiModelProperty("用戶ID") //增加字段接口注釋
  private Integer id;
  @ApiModelProperty("用戶名")
  private String username;
}

測(cè)試控制器

SwaggerController.java

package com.example.demo.controller;


import com.example.demo.entity.User;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SwaggerController {


  @GetMapping("/hello")
  public String hello(){
    return "hello";
  }


  /**
   * 接口返回值含有實(shí)體類(lèi),就會(huì)被swagger掃描
   *
   * @return
   */
  @ApiOperation("查詢用戶方法注釋")
  @GetMapping(value = "/get/{id}")
  public User get(@ApiParam("請(qǐng)求參數(shù)注釋") @PathVariable(value = "id")Integer id){
    return new User();
  }
}

使用dev環(huán)境 啟動(dòng)項(xiàng)目后 瀏覽器打開(kāi)http://localhost:8081/swagger-ui.html#/ 我這里用的端口是8081

顯示效果

SpringBoot整合Swagger框架的方法

看完上述內(nèi)容,是不是對(duì)SpringBoot整合Swagger框架的方法有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享名稱:SpringBoot整合Swagger框架的方法
本文網(wǎng)址:http://bm7419.com/article40/jdeseo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)響應(yīng)式網(wǎng)站、網(wǎng)頁(yè)設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化、App設(shè)計(jì)、面包屑導(dǎo)航

廣告

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

成都做網(wǎng)站