溫馨提示×

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

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

SpringBoot基于Swagger2怎么構(gòu)建API文檔

發(fā)布時(shí)間:2022-04-07 11:14:07 來(lái)源:億速云 閱讀:95 作者:iii 欄目:編程語(yǔ)言

本文小編為大家詳細(xì)介紹“SpringBoot基于Swagger2怎么構(gòu)建API文檔”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“SpringBoot基于Swagger2怎么構(gòu)建API文檔”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。

一、添加依賴(lài)

<!--SpringBoot使用Swagger2構(gòu)建API文檔的依賴(lài)-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.7.0</version>
    </dependency>

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.7.0</version>
    </dependency>

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

package com.offcn.config;

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;

@Configuration//表示該類(lèi)為一個(gè)配置類(lèi),相當(dāng)于spring中的xml配置文件
@EnableSwagger2 //開(kāi)啟在線文檔
public class SwaggerConfig {

  //1.聲明 api 文檔的屬性
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        .title("Spring Boot中使用Swagger2構(gòu)建RESTful APIs")
        .description("優(yōu)就業(yè)")
        .termsOfServiceUrl("http://www.ujiuye.com/")
        .contact("小劉同學(xué)")
        .version("1.0")
        .build();
  }

  //配置核心配置信息
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo())
        .select()
        .apis(RequestHandlerSelectors.basePackage("com.offcn.controller"))
        .paths(PathSelectors.any())
        .build();
  }
}

三、修改Controller 增加文檔注釋

通過(guò)@ApiOperation注解來(lái)給API增加說(shuō)明

通過(guò)@ApiImplicitParams@ApiImplicitParam注解來(lái)給參數(shù)增加說(shuō)明

package com.offcn.controller;

import com.offcn.dao.UserDao;
import com.offcn.entity.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/rest")
@RestController
public class RestFulController {

  @Autowired
  private UserDao userDao;

  @GetMapping("/getUserById")
  @ApiOperation(value="查找指定id用戶(hù)信息", notes="根據(jù)id查找用戶(hù)信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "用戶(hù)ID", required = true, dataType = "Integer"),
  })
  public User getUserById(Integer id){
    User user = userDao.getOne(id);
    return user;
  }

  @DeleteMapping("/del")
  @ApiOperation(value="刪除指定id用戶(hù)信息", notes="根據(jù)id刪除用戶(hù)信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "用戶(hù)ID", required = true, dataType = "Integer"),
  })
  public String delUserById(Integer id){
    userDao.deleteById(id);
    return "success";
  }
}

SpringBoot基于Swagger2怎么構(gòu)建API文檔

四、查看Swagger2文檔

重啟項(xiàng)目

訪問(wèn):

http://localhost:8080/swagger-ui.html

讀到這里,這篇“SpringBoot基于Swagger2怎么構(gòu)建API文檔”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

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

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

AI