最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在SpringBoot項(xiàng)目中的使用Swagger的方法示例

 更新時(shí)間:2021年05月12日 11:11:53   作者:小鄭要做干飯人  
這篇文章主要介紹了在SpringBoot項(xiàng)目中的使用Swagger的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

一. 首先Swagger是什么?

在這里插入圖片描述

Swagger 是一個(gè)規(guī)范和完整的框架,用于生成、描述、調(diào)用和可視化 RESTful 風(fēng)格的 Web 服務(wù)??傮w目標(biāo)是使客戶端和文件系統(tǒng)作為服務(wù)器以同樣的速度來(lái)更新。文件的方法,參數(shù)和模型緊密集成到服務(wù)器端的代碼,允許API來(lái)始終保持同步。Swagger官方API文檔:https://swagger.io/

作用:
  1. 接口的文檔在線自動(dòng)生成。
  2. 功能測(cè)試。

Swagger的主見介紹:

在這里插入圖片描述

   Swagger Codegen: 通過(guò)Codegen 可以將描述文件生成html格式和cwiki形式的接口文檔,同時(shí)也能生成多鐘語(yǔ)言的服務(wù)端和客戶端的代碼。支持通過(guò)jar包,docker,node等方式在本地化執(zhí)行生成。也可以在后面的Swagger Editor中在線生成。

  Swagger UI: 提供了一個(gè)可視化的UI頁(yè)面展示描述文件。接口的調(diào)用方、測(cè)試、項(xiàng)目經(jīng)理等都可以在該頁(yè)面中對(duì)相關(guān)接口進(jìn)行查閱和做一些簡(jiǎn)單的接口請(qǐng)求。該項(xiàng)目支持在線導(dǎo)入描述文件和本地部署UI項(xiàng)目。

  Swagger Editor: 類似于markendown編輯器的編輯Swagger描述文件的編輯器,該編輯支持實(shí)時(shí)預(yù)覽描述文件的更新效果。也提供了在線編輯器和本地部署編輯器兩種方式。

  Swagger Inspector: 感覺(jué)和postman差不多,是一個(gè)可以對(duì)接口進(jìn)行測(cè)試的在線版的postman。比在Swagger UI里面做接口請(qǐng)求,會(huì)返回更多的信息,也會(huì)保存你請(qǐng)求的實(shí)際請(qǐng)求參數(shù)等數(shù)據(jù)。

  Swagger Hub: 集成了上面所有項(xiàng)目的各個(gè)功能,你可以以項(xiàng)目和版本為單位,將你的描述文件上傳到Swagger Hub中。在Swagger Hub中可以完成上面項(xiàng)目的所有工作,需要注冊(cè)賬號(hào),分免費(fèi)版和收費(fèi)版。

PS:
  Springfox Swagger: Spring 基于 swagger 規(guī)范,可以將基于 SpringMVC 和 Spring Boot 項(xiàng)目的項(xiàng)目代碼,自動(dòng)生成 JSON 格式的描述文件。本身不是屬于 Swagger 官網(wǎng)提供的,在這里列出來(lái)做個(gè)說(shuō)明,方便后面作一個(gè)使用的展開。

二. Swagger UI的使用:

Swagger注解解釋:

@Api:請(qǐng)求類的說(shuō)明
@Api:放在 請(qǐng)求的類上,與 @Controller 并列,說(shuō)明類的作用,如用戶模塊,訂單類等。
tags="說(shuō)明該類的作用"
value="該參數(shù)沒(méi)什么意義,所以不需要配置"

常用注解:
- @Api()用于類;
表示標(biāo)識(shí)這個(gè)類是swagger的資源
- @ApiOperation()用于方法;
表示一個(gè)http請(qǐng)求的操作
- @ApiParam()用于方法,參數(shù),字段說(shuō)明;
表示對(duì)參數(shù)的添加元數(shù)據(jù)(說(shuō)明或是否必填等)
- @ApiModel()用于類
表示對(duì)類進(jìn)行說(shuō)明,用于參數(shù)用實(shí)體類接收
- @ApiModelProperty()用于方法,字段
表示對(duì)model屬性的說(shuō)明或者數(shù)據(jù)操作更改
- @ApiIgnore()用于類,方法,方法參數(shù)
表示這個(gè)方法或者類被忽略
- @ApiImplicitParam() 用于方法
表示單獨(dú)的請(qǐng)求參數(shù)
- @ApiImplicitParams() 用于方法
包含多個(gè) @ApiImplicitParam

(1). @Api: 請(qǐng)求類的說(shuō)明

@Api:放在 請(qǐng)求的類上,與 @Controller 并列,說(shuō)明類的作用,如用戶模塊,訂單類等。
tags="說(shuō)明該類的作用"
value="描述該類作用" [推薦用這個(gè)]

@Api 其它屬性配置:

屬性名稱 備注
value url的路徑值
tags 如果設(shè)置這個(gè)值、value的值會(huì)被覆蓋
description 對(duì)api資源的描述
basePath 基本路徑
position 如果配置多個(gè)Api 想改變顯示的順序位置
produces 如, “application/json, application/xml”
consumes 如, “application/json, application/xml”
protocols 協(xié)議類型,如: http, https, ws, wss.
authorizations 高級(jí)特性認(rèn)證時(shí)配置
hidden 配置為true ,將在文檔中隱藏


(2). @ApiOperation:方法的說(shuō)明

@ApiOperation:"用在請(qǐng)求的方法上,說(shuō)明方法的作用"
	  value="說(shuō)明方法的作用"
	  notes="方法的備注說(shuō)明"

(3). @ApiImplicitParams、@ApiImplicitParam:方法參數(shù)的說(shuō)明

@ApiImplicitParams:用在請(qǐng)求的方法上,包含一組參數(shù)說(shuō)明
@ApiImplicitParam:對(duì)單個(gè)參數(shù)的說(shuō)明
name:參數(shù)名
value:參數(shù)的漢字說(shuō)明、解釋
required:參數(shù)是否必須傳
paramType:參數(shù)放在哪個(gè)地方
· header --> 請(qǐng)求參數(shù)的獲?。篅RequestHeader
· query --> 請(qǐng)求參數(shù)的獲取:@RequestParam
· path(用于restful接口)--> 請(qǐng)求參數(shù)的獲?。篅PathVariable
· body(請(qǐng)求體)--> @RequestBody User user
· form(普通表單提交)
dataType:參數(shù)類型,默認(rèn)String,其它值dataType="Integer"
defaultValue:參數(shù)的默認(rèn)值

示例:

@Api(tags="用戶模塊")
@Controller
public class UserController {

	@ApiOperation(value="用戶登錄",notes="隨邊說(shuō)點(diǎn)啥")
	@ApiImplicitParams({
		@ApiImplicitParam(name="mobile",value="手機(jī)號(hào)",required=true,paramType="form"),
		@ApiImplicitParam(name="password",value="密碼",required=true,paramType="form"),
		@ApiImplicitParam(name="age",value="年齡",required=true,paramType="form",dataType="Integer")
	})
	@PostMapping("/login")
	public JsonResult login(@RequestParam String mobile, @RequestParam String password,
	@RequestParam Integer age){
		//...
	    return JsonResult.ok(map);
	}
}

(4). @ApiResponses、@ApiResponse:方法返回值的說(shuō)明

@ApiResponses:方法返回對(duì)象的說(shuō)明
	@ApiResponse:每個(gè)參數(shù)的說(shuō)明
	    code:數(shù)字,例如400
	    message:信息,例如"請(qǐng)求參數(shù)沒(méi)填好"
	    response:拋出異常的類

(5). @ApiModel:用于JavaBean上面,表示一個(gè)JavaBean(如:響應(yīng)數(shù)據(jù))的信息

@ApiModel:用于JavaBean的類上面,表示此 JavaBean 整體的信息
			(這種一般用在post創(chuàng)建的時(shí)候,使用 @RequestBody 這樣的場(chǎng)景,
			請(qǐng)求參數(shù)無(wú)法使用 @ApiImplicitParam 注解進(jìn)行描述的時(shí)候 )

(6). @ApiModelProperty:用在JavaBean類的屬性上面,說(shuō)明屬性的含義

@ApiModel(description= "返回響應(yīng)數(shù)據(jù)")
public class RestMessage implements Serializable{

	@ApiModelProperty(value = "是否成功")
	private boolean success=true;
	@ApiModelProperty(value = "返回對(duì)象")
	private Object data;
	@ApiModelProperty(value = "錯(cuò)誤編號(hào)")
	private Integer errCode;
	@ApiModelProperty(value = "錯(cuò)誤信息")
	private String message;
		
	/* getter/setter 略*/
}

三. Swagger整合SpringBoot

1. Pom依賴:

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

2. 配置類:

package com.zhiyou100.configBeans;

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;

/**
 * @Author ZhengZiXuan
 * @Desc
 */
@Configuration // @Configuration注解,讓Spring來(lái)加載該類配置。
@EnableSwagger2 //@EnableSwagger2注解來(lái)啟用Swagger2
public class SwaggerConfigBean {
    /**
     * 創(chuàng)建API應(yīng)用
     * apiInfo() 增加API相關(guān)信息
     * 通過(guò)select()函數(shù)返回一個(gè)ApiSelectorBuilder實(shí)例,用來(lái)控制哪些接口暴露給Swagger來(lái)展現(xiàn),
     * 本例采用指定掃描的包路徑來(lái)定義指定要建立API的目錄。
     *
     * @return
     */
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zhiyou100.controller"))
                .paths(PathSelectors.any())
                .build();
    }
    /**
     * 創(chuàng)建該API的基本信息(這些基本信息會(huì)展現(xiàn)在文檔頁(yè)面中)
     * 訪問(wèn)地址:http://項(xiàng)目實(shí)際地址/swagger-ui.html
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("使用Swagger2 構(gòu)建RESTful APIS - 廢物")
                .description("廢物 - Swagger使用演示")
                .termsOfServiceUrl("www.zzxBIuBIuBIu.com")
                .version("1.0")
                .build();
    }

}

3. 正常的Controller再加Swagger注解:

package com.zhiyou100.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author ZhengZiXuan
 * @Desc
 */
@Controller
@Api("測(cè)試Swagger 的Controller類")
public class TestController {
    @ApiOperation(value="根據(jù)id查詢名字",notes = "備注:無(wú)")
    @ApiImplicitParam(name = "id",value = "用戶id",required = true,
            paramType = "query",dataType = "Integer")
    @RequestMapping("/getNameById")
    @ResponseBody
    public String getNameById(int id){
        return "張三";
    }
}

4.啟動(dòng)測(cè)試:

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

四. 訪問(wèn)404Bug的解決方法

在這里插入圖片描述

Swagger UI 界面介紹:

在這里插入圖片描述

在這里插入圖片描述

五. Model對(duì)象增刪改查演示

對(duì)User類實(shí)現(xiàn)增刪改查,生成api文檔

package com.zhiyou100.model;

/**
 * @Author ZhengZiXuan
 * @Date 2021/05/10
 * @Desc
 */

public class User {

    private int id;
    private String name;
    private String password;

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                '}';
    }

    public User() {
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public User(int id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password;
    }
}
package com.zhiyou100.controller;

import com.zhiyou100.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author ZhengZiXuan
 * @Date 2021/05/10
 * @Desc  
有點(diǎn)BUG  就是list集合中的增刪改
 */
@RestController
@Api("User類的增刪改查API")
public class UserController {
    ArrayList<User> users = null;
    public UserController(){
        users = new ArrayList<>();
        users.add(new User(1,"張三","123"));
        users.add(new User(2,"李四","1234"));
        users.add(new User(3,"王五","12345"));
        System.out.println("init users "+users);
    }
    /**
     * 根據(jù)id查User
     */
    @ApiOperation("根據(jù)id查用戶")
    @ApiImplicitParam(name = "id",value = "用戶id",
                      required = true,paramType = "path",
                        dataType = "Integer")
    @RequestMapping(value="/user/get/{id}",method = RequestMethod.GET)
    public User getUserById(@PathVariable("id") Integer id){
        System.out.println("getUserById id: "+id);
        if (id != null){
            if (id>0 && id <4){
                User user = users.get(id);
                System.out.println("getUserById User: "+user);
                return user;
            }
        }
        return null;
    }
    /**
     * 查全部user
     */
    @ApiOperation("查詢?nèi)坑脩?)
    @RequestMapping(value="/user/get",method = RequestMethod.GET)
    public List<User> getAllUser(){
        System.out.println("getAllUser");
        return users;
    }

    /**
     * 添加User
     */
    @ApiOperation("添加用戶")
    @ApiImplicitParam(name="user",value = "用戶對(duì)象",paramType = "body",dataType = "User")
    @RequestMapping(value="/user/add",method = RequestMethod.POST)
    public User addUser(@RequestBody User user){
        System.out.println("addUser User:"+user);
        if (user == null || user.getId() == 0){
            return null;
        }
        users.add(user);
        return user;
    }

    /**
     * 刪除User
     */
    @ApiOperation("根據(jù)id刪除用戶")
    @ApiImplicitParam(name = "id",value = "用戶id",
            required = true,paramType = "path",
            dataType = "Integer")
    @RequestMapping(value="/user/delete/{id}",method = RequestMethod.DELETE)
    public boolean delUserById(@PathVariable("id") Integer id){
        System.out.println("delUserById id:"+id);
        if (id != null){
            users.remove(id);
            return true;
        }
        return false;
    }

    /**
     * 更新User
     */
    @ApiOperation("根據(jù)id更新用戶")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用戶id",
                    required = true,paramType = "path",
                    dataType = "Integer"),
            @ApiImplicitParam(name = "user",value = "用戶對(duì)象",
                    required = true,paramType = "body",
                    dataType = "User")})
    @RequestMapping(value="/user/update/{id}",method = RequestMethod.PUT)
    public boolean UpdateUserById(@PathVariable("id") Integer id,@RequestBody User user){
        System.out.println("UpdateUserById id:"+id+"  ,   User:"+user);
        if (id != null && user != null){
            users.add(id,user);
            return true;
        }
        return false;
    }

}

在這里插入圖片描述

1. 查詢?nèi)?

在這里插入圖片描述

2. 根據(jù)id查:

在這里插入圖片描述

3. 添加用戶:

在這里插入圖片描述

在這里插入圖片描述

4.更新用戶:

在這里插入圖片描述

5. 刪除用戶:

在這里插入圖片描述

到此這篇關(guān)于在SpringBoot項(xiàng)目中的使用Swagger的方法示例的文章就介紹到這了,更多相關(guān)SpringBoot使用Swagger內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • spring.profiles.active配置使用小結(jié)

    spring.profiles.active配置使用小結(jié)

    spring.profiles.active?配置使得應(yīng)用程序能夠在不同的環(huán)境中使用不同的配置,本文主要介紹了spring.profiles.active配置使用小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-07-07
  • java.lang.OutOfMemoryError: Metaspace異常解決的方法

    java.lang.OutOfMemoryError: Metaspace異常解決的方法

    這篇文章主要介紹了java.lang.OutOfMemoryError: Metaspace異常解決的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • SpringCloud升級(jí)2020.0.x版之OpenFeign簡(jiǎn)介與使用實(shí)現(xiàn)思路

    SpringCloud升級(jí)2020.0.x版之OpenFeign簡(jiǎn)介與使用實(shí)現(xiàn)思路

    在微服務(wù)系統(tǒng)中,我們經(jīng)常會(huì)進(jìn)行 RPC 調(diào)用。在 Spring Cloud 體系中,RPC 調(diào)用一般就是 HTTP 協(xié)議的調(diào)用。對(duì)于每次調(diào)用,都要經(jīng)過(guò)一系列詳細(xì)步驟,接下來(lái)通過(guò)本文給大家介紹SpringCloud OpenFeign簡(jiǎn)介與使用,感興趣的朋友一起看看吧
    2021-10-10
  • 一文詳解Java攔截器與過(guò)濾器的使用

    一文詳解Java攔截器與過(guò)濾器的使用

    這篇文章主要為大家詳細(xì)介紹了Java中攔截器與過(guò)濾器的使用與區(qū)別,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)有一定參考價(jià)值,需要的可以參考一下
    2022-04-04
  • 詳解Spring循環(huán)依賴的解決方案

    詳解Spring循環(huán)依賴的解決方案

    這篇文章主要介紹了詳解Spring循環(huán)依賴的解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • 詳解SpringBoot獲得Maven-pom中版本號(hào)和編譯時(shí)間戳

    詳解SpringBoot獲得Maven-pom中版本號(hào)和編譯時(shí)間戳

    這篇文章主要介紹了詳解SpringBoot獲得Maven-pom中版本號(hào)和編譯時(shí)間戳,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Spring Boot 使用Druid詳解

    Spring Boot 使用Druid詳解

    本篇文章主要介紹了Spring Boot 使用Druid配置詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-05-05
  • java實(shí)現(xiàn)線上環(huán)境遠(yuǎn)程debug調(diào)試過(guò)程

    java實(shí)現(xiàn)線上環(huán)境遠(yuǎn)程debug調(diào)試過(guò)程

    本文介紹了在Docker容器中配置Tomcat和Spring Boot進(jìn)行遠(yuǎn)程調(diào)試的方法,通過(guò)配置`CATALINA_OPTS`或`JPDA`參數(shù),可以在測(cè)試環(huán)境中啟動(dòng)Tomcat或Spring Boot應(yīng)用的debug模式,然后在本地IDEA中設(shè)置斷點(diǎn)進(jìn)行調(diào)試,從而更高效地定位和解決問(wèn)題
    2025-02-02
  • 學(xué)習(xí)Java之如何對(duì)時(shí)間進(jìn)行格式化

    學(xué)習(xí)Java之如何對(duì)時(shí)間進(jìn)行格式化

    當(dāng)我們?cè)谀J(rèn)情況下構(gòu)造出來(lái)的時(shí)間對(duì)象,它的時(shí)間格式并不適合我們閱讀,并且在開發(fā)時(shí),pc端、Android端、iOS端等展示的時(shí)間格式可能也并不完全一樣,本文就從這幾個(gè)問(wèn)題給大家介紹如何對(duì)時(shí)間進(jìn)行格式化,感興趣的同學(xué)可以借鑒一下
    2023-05-05
  • Java8中時(shí)區(qū)與不同歷法處理指南

    Java8中時(shí)區(qū)與不同歷法處理指南

    Java?8?的?java.time?API?不僅修復(fù)了舊版日期時(shí)間?API?的設(shè)計(jì)缺陷,還提供了對(duì)時(shí)區(qū)和多歷法的全面支持,下面小編就來(lái)講講具體的處理操作,有需要的可以了解下
    2025-04-04

最新評(píng)論

福贡县| 乌审旗| 监利县| 屯门区| 长泰县| 通州区| 翁源县| 巴里| 桑植县| 贵溪市| 枣强县| 湘西| 丹江口市| 定远县| 黔西县| 宁晋县| 古田县| 嘉峪关市| 芜湖市| 无极县| 平和县| 吴川市| 唐海县| 德清县| 郑州市| 尉氏县| 漳浦县| 梨树县| 乳源| 九龙城区| 太湖县| 沙河市| 云南省| 高安市| 元氏县| 长顺县| 雷州市| 泰州市| 武功县| 新干县| 班玛县|