欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot整合swagger的操作指南

 更新時(shí)間:2023年09月24日 10:07:04   作者:fking86  
Swagger 是一個(gè)開源的框架,用于設(shè)計(jì)、構(gòu)建、文檔化和使用 RESTful 風(fēng)格的 Web 服務(wù),Spring Boot 是一個(gè)用于構(gòu)建獨(dú)立的、基于生產(chǎn)級(jí)別的 Spring 應(yīng)用程序的框架,本文講給大家介紹一下SpringBoot整合swagger的操作指南,需要的朋友可以參考下

簡介

Swagger 是一個(gè)開源的框架,用于設(shè)計(jì)、構(gòu)建、文檔化和使用 RESTful 風(fēng)格的 Web 服務(wù)。Spring Boot 是一個(gè)用于構(gòu)建獨(dú)立的、基于生產(chǎn)級(jí)別的 Spring 應(yīng)用程序的框架。它可以集成 Swagger 來簡化 RESTful API 的開發(fā)和文檔生成。

通過集成 Swagger,你可以在 Spring Boot 應(yīng)用程序中自動(dòng)生成 API 文檔,這些文檔描述了你的 API 的各種端點(diǎn)、請(qǐng)求參數(shù)、響應(yīng)等詳細(xì)信息。Swagger 還提供了一個(gè)交互式 UI,讓開發(fā)人員可以方便地瀏覽和測(cè)試 API。

實(shí)例

版本依賴

JDK17

SpringBoot 2.5.4

swagger 3.0.0

knife4j-spring-boot-starter 3.0.2

引入依賴

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.5.4</version>
</parent>
<groupId>com.example</groupId>
<artifactId>springboot-swagger</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
    <java.version>17</java.version>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--引入spring web -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--引入swagger3 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version>
    </dependency>
    <!--Knife4j-->
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
        <version>3.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>provided</scope>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.example.SpringbootSwaggerSampleApplication</mainClass>
            </configuration>
        </plugin>
    </plugins>
</build>

SwaggerConfig

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Response;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.ArrayList;
import java.util.List;
/**
 * @Description: (Swagger配置)
 * @Date 2023/09/07
 */
@Configuration
@EnableOpenApi
@EnableKnife4j
public class SwaggerConfig {
    /**
     * swagger3的配置文件
     */
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .apis(RequestHandlerSelectors.basePackage("com.example.controller"))
                .paths(PathSelectors.any())
                .build()
                //.globalRequestParameters(getGlobalRequestParameters())
                .globalResponses(HttpMethod.GET, getGlobalResponseMessage())
                .globalResponses(HttpMethod.POST, getGlobalResponseMessage())
                .globalResponses(HttpMethod.DELETE, getGlobalResponseMessage())
                .globalResponses(HttpMethod.PUT, getGlobalResponseMessage());
    }
    /**
     * 構(gòu)建 api文檔的詳細(xì)信息函數(shù),注意這里的注解引用的是哪個(gè)
     */
    private ApiInfo apiInfo() {
        // 獲取工程名稱
        String projectName = System.getProperty("user.dir");
        return new ApiInfoBuilder()
                .title(projectName.substring(projectName.lastIndexOf("\\") + 1) + " API接口文檔")
                .contact(new Contact("coderJim", "https://www.example.com", "coderJim@163.com"))
                .version("1.0")
                .description("API文檔")
                .build();
    }
    /**
     * 生成通用響應(yīng)信息
     *
     * @return
     */
    private List<Response> getGlobalResponseMessage() {
        List<Response> responseList = new ArrayList<>();
        responseList.add(new ResponseBuilder().code("404").description("找不到資源").build());
        return responseList;
    }
}

User

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.ToString;
/**
 * @Description: (用一句話描述該文件做什么)
 * @date 2023/09/07
 */
@Data
@ToString
@ApiModel(value = "用戶實(shí)體類", description = "用戶信息描述類")
public class User {
    @ApiModelProperty(value = "用戶id")
    private Integer id;
    @ApiModelProperty(value = "用戶名")
    private String username;
    @ApiModelProperty(value = "用戶密碼")
    private String password;
}

UserController

import com.example.entity.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
/**
 *
 * @Package com.example.controller
 * @Description: (用一句話描述該文件做什么)
 * @date 2023/09/07
 */
@RestController
@Api(tags = "用戶數(shù)據(jù)接口") // @Api 注解標(biāo)注在類上用來描述整個(gè) Controller 信息。
public class UserController {
    /**
     * @ApiOperation 注解標(biāo)注在方法上,用來描述一個(gè)方法的基本信息。
     */
    @ApiOperation(value = "修改用戶", notes = "傳入用戶信息進(jìn)行更新修改")
    @PutMapping("/user")
    public String updateUser(@RequestBody User user){
        return user.toString();
    }
    /**
     * @ApiImplicitParam 注解標(biāo)注在方法上,用來描述方法的參數(shù)。其中 paramType 是指方法參數(shù)的類型,有如下可選值:
     * path:參數(shù)獲取方式為 @PathVariable
     * query:參數(shù)獲取方式為 @RequestParam
     * header:參數(shù)獲取方式為 @RequestHeader
     * body
     * form
     */
    @ApiOperation(value ="查詢用戶", notes = "根據(jù) id 查詢用戶")
    @ApiImplicitParam(paramType = "path", name = "id", value = "用戶 id", required = true)
    @GetMapping("/user/{id}")
    public String getUserById(@PathVariable Integer id){
        return "查找的用戶id是:" + id;
    }
    /**
     * 如果有多個(gè)參數(shù),可以將多個(gè)參數(shù)的 @ApiImplicitParam 注解放到 @ApiImplicitParams 中
     */
    @ApiOperation(value = "新增用戶", notes = "根據(jù)傳入的用戶名和密碼添加一個(gè)新用戶")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "username",
                    value = "用戶名", required = true, defaultValue = "test"),
            @ApiImplicitParam(paramType = "query", name = "password",
                    value = "密碼", required = true, defaultValue = "123")
    })
    @PostMapping("/user")
    public String addUser(@RequestParam String username, @RequestParam String password) {
        return "新增用戶:" + username + " " + password;
    }
    /**
     * @ApiResponse 是對(duì)響應(yīng)結(jié)果的描述。code 表示響應(yīng)碼,message 為相應(yīng)的描述信息。如果有多個(gè) @ApiResponse,則放在一個(gè) @ApiResponses 中
     */
    @ApiOperation(value = "刪除用戶", notes = "根據(jù) id 刪除用戶")
    @ApiResponses({
            @ApiResponse(code = 200, message = "刪除成功!"),
            @ApiResponse(code = 500, message = "刪除失?。?)
    })
    @DeleteMapping("/user/{id}")
    public Integer deleteUserById(@PathVariable Integer id) {
        return id;
    }
    /**
     * @ApiIgnore 注解表示不對(duì)某個(gè)接口生成文檔。
     */
    @ApiIgnore
    @GetMapping("/user/test")
    public String test() {
        return "這是一個(gè)測(cè)試接口,不需要在api文檔中顯示。";
    }
}

測(cè)試結(jié)果

啟動(dòng)打開鏈接:http://127.0.0.1:8080/swagger-ui/index.html

打開鏈接:http://127.0.0.1:8080/doc.html

以上就是SpringBoot整合swagger的操作指南的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot整合swagger的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring?Boot應(yīng)用中如何動(dòng)態(tài)指定數(shù)據(jù)庫實(shí)現(xiàn)不同用戶不同數(shù)據(jù)庫的問題

    Spring?Boot應(yīng)用中如何動(dòng)態(tài)指定數(shù)據(jù)庫實(shí)現(xiàn)不同用戶不同數(shù)據(jù)庫的問題

    讓我們創(chuàng)建一個(gè) Spring Boot 項(xiàng)目首先設(shè)置一個(gè)具有必要依賴項(xiàng)的新 Spring Boot項(xiàng)目,在項(xiàng)目配置中包括 Spring Web、Spring Data JPA 和關(guān)于數(shù)據(jù)庫的依賴項(xiàng),接下來介紹Spring?Boot應(yīng)用中如何動(dòng)態(tài)指定數(shù)據(jù)庫,實(shí)現(xiàn)不同用戶不同數(shù)據(jù)庫的場(chǎng)景?,需要的朋友可以參考下
    2024-04-04
  • Spring Cloud中使用Eureka的詳細(xì)過程

    Spring Cloud中使用Eureka的詳細(xì)過程

    Eureka 是 Netflix 開源的一個(gè)服務(wù)發(fā)現(xiàn)組件,它在微服務(wù)架構(gòu)中扮演著重要的角色,這篇文章主要介紹了Spring Cloud中如何使用Eureka,需要的朋友可以參考下
    2024-07-07
  • Springboot處理跨域的實(shí)現(xiàn)方式(附Demo)

    Springboot處理跨域的實(shí)現(xiàn)方式(附Demo)

    這篇文章主要介紹了Springboot處理跨域的實(shí)現(xiàn)方式(附Demo),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • 在idea中g(shù)it pull失敗的解決方案

    在idea中g(shù)it pull失敗的解決方案

    在遇到Git Pull失敗時(shí),首先使用IDEA的git-revert功能進(jìn)行還原,然后檢查并解決分支沖突,最后重新執(zhí)行Git Pull確保所有文件是最新的,注意,在操作過程中確保網(wǎng)絡(luò)連接正常,并且每步操作后都要執(zhí)行Git Pull來更新數(shù)據(jù)
    2024-10-10
  • Mybatis-plus如何在xml中傳入自定義的SQL語句

    Mybatis-plus如何在xml中傳入自定義的SQL語句

    這篇文章主要介紹了Mybatis-plus如何在xml中傳入自定義的SQL語句問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java如何防止JS腳本注入代碼實(shí)例

    Java如何防止JS腳本注入代碼實(shí)例

    這篇文章主要介紹了Java如何防止JS腳本注入代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • SpringBoot使用Async注解失效原因分析及解決(spring異步回調(diào))

    SpringBoot使用Async注解失效原因分析及解決(spring異步回調(diào))

    這篇文章主要介紹了SpringBoot使用Async注解失效原因分析及解決(spring異步回調(diào)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 新手學(xué)習(xí)微服務(wù)SpringCloud項(xiàng)目架構(gòu)搭建方法

    新手學(xué)習(xí)微服務(wù)SpringCloud項(xiàng)目架構(gòu)搭建方法

    這篇文章主要介紹了新手學(xué)習(xí)微服務(wù)SpringCloud項(xiàng)目架構(gòu)搭建方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • 如何使用Spring Batch進(jìn)行批處理任務(wù)管理

    如何使用Spring Batch進(jìn)行批處理任務(wù)管理

    本文介紹了如何配置Spring Batch、如何創(chuàng)建批處理任務(wù),以及如何讀取和寫入數(shù)據(jù),希望通過本文的介紹,你能更好地理解和使用Spring Batch來管理批處理任務(wù),感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • Java的代理模式你真的了解嗎

    Java的代理模式你真的了解嗎

    這篇文章主要為大家詳細(xì)介紹了Java的代理模式,結(jié)構(gòu)型模式主要總結(jié)了一些類或?qū)ο蠼M合在一起的經(jīng)典結(jié)構(gòu),這些經(jīng)典的結(jié)構(gòu)可以解決特定應(yīng)用場(chǎng)景的問題,包括:代理模式、橋接模式、裝飾器模式、適配器模式、門面模式、組合模式、享元模式
    2022-03-03

最新評(píng)論