日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot + Swagger

發布時間:2024/4/14 javascript 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot + Swagger 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

http://springfox.github.io/springfox/docs/current/#customizing-the-swagger-endpoints

一、什么是Swagger?

Swagger:? 一個描述Restful service規范

?????????????? 可以在界面上測試RestfulService

  • swagger-ui: 用來顯示API文檔
  • swagger-editor: 就是一個在線編輯文檔說明文件(swagger.json或swagger.yaml文件)的工具,以方便生態中的其他小工具(swagger-ui)等使用
  • swagger-validator: 這個小工具是用來校驗生成的文檔說明文件是否符合語法規定的。用法非常簡單,只需url地址欄,根路徑下加上一個參數url,參數內容是放swagger說明文件的地址。即可校驗。
  • swagger-codegen: 代碼生成器,腳手架。可以根據swagger.json或者swagger.yml文件生成指定的計算機語言指定框架的代碼。
  • ?

    二、Swagger 和 Springfox-Swagger

    What is the relationship between swagger-ui and springfox-swagger-ui?
    ??? Swagger Spec is a specification.
    ??? Swagger Api - an implementation of that specification that supports jax-rs, restlet, jersey etc.
    ??? Springfox libraries in general - another implementation of the specification focused on the spring based ecosystem.
    ??? Swagger.js and Swagger-ui - are client libraries in javascript that can consume swagger specification.
    ??? springfox-swagger-ui - the one that you’re referring to, is just packaging swagger-ui in a convenient way so that spring services can serve it up.

    翻譯下來就是:
    swagger-ui 和 springfox-swagger-ui 的關系是?
    ??? Swagger Spec 是一個規范。
    ??? Swagger Api 是 Swagger Spec 規范 的一個實現,它支持 jax-rs, restlet, jersey 等等。
    ??? Springfox libraries 是 Swagger Spec 規范 的另一個實現,專注于 spring 生態系統。
    ??? Swagger.js and Swagger-ui 是 javascript 的客戶端庫,能消費該規范。
    ??? springfox-swagger-ui 僅僅是以一種方便的方式封裝了 swagger-ui ,就是可以顯示文檔內容

    ?

    三、Springboot 與Swagger集成

    1. 增加下面maven 引用

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

    ?

    2. 增加一個Swaagger configuration

    ?

    package com.example.config;import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;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;@Configuration public class Swagger2UiConfiguration extends WebMvcConfigurerAdapter {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("org.springframework.boot")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("springboot利用swagger構建api文檔").description("簡單優雅的restful風格,http://XX").termsOfServiceUrl("http://XXx").version("1.0").build();}}

    ?3.Spring Boots Application 類增加 @EnableSwagger2

    ?

    ?

    import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication @EnableSwagger2 public class UserApplication1 {public static void main(String[] args) {SpringApplication.run(UserApplication1.class, args);} }

    ?4. 在Controller 中增加相對應的API注釋

    package com.example.demo;import java.util.concurrent.atomic.AtomicLong;import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController;import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiOperation;@RestController public class GreetingController {private static final String template = "Hello, %s!";private final AtomicLong counter = new AtomicLong();@ApiOperation(value="獲取Greeting info", notes="根據name獲取greeting info")@ApiImplicitParam(name = "name", value = "用戶名", required = true, dataType = "String", paramType = "path")@RequestMapping("/greeting")public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {return new Greeting(counter.incrementAndGet(), String.format(template, name));} }

    ?部署啟動服務器

    訪問: http://localhost:8080/swagger-ui.html#

    ?

    四、Swagger注解

    ?

    swagger通過注解表明該接口會生成文檔,包括接口名、請求方法、參數、返回信息的等等。

    ?

    • @Api:修飾整個類,描述Controller的作用
    • @ApiOperation:描述一個類的一個方法,或者說一個接口
    • @ApiParam:單個參數描述
    • @ApiModel:用對象來接收參數
    • @ApiProperty:用對象接收參數時,描述對象的一個字段
    • @ApiResponse:HTTP響應其中1個描述
    • @ApiResponses:HTTP響應整體描述
    • @ApiIgnore:使用該注解忽略這個API
    • @ApiError :發生錯誤返回的信息
    • @ApiImplicitParam:一個請求參數
    • @ApiImplicitParams:多個請求參數

    總結

    以上是生活随笔為你收集整理的Spring Boot + Swagger的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。