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

Java詳解swagger2如何配置使用

 更新時間:2022年06月30日 09:08:52   作者:念舊、sunshine  
編寫和維護接口文檔是每個程序員的職責,根據(jù)Swagger2可以快速幫助我們編寫最新的API接口文檔,再也不用擔心開會前仍忙于整理各種資料了,間接提升了團隊開發(fā)的溝通效率

swagger可能會遇到的問題:

當我們的接口返回的數(shù)據(jù)太大的時候,swagger會崩潰

1、引入jar包

 <!--接口文檔-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.10.5</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework.plugin</groupId>
                    <artifactId>spring-plugin-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.6.0</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.6.0</version>
        </dependency>

2、啟用swagger application.properties

不用了

#swagger開關
swagger.enable=true

3、配置文件

package com.zkhx.config;
import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import java.util.ArrayList;
import java.util.List;
@Configuration // 標明是配置類
@EnableSwagger2WebMvc 
@EnableKnife4j
@Import(BeanValidatorPluginsConfiguration.class)
public class SwaggerConfig {
    /**
     * Create rest api docket.
     *
     * @return the docket
     */
    @Bean
    public Docket createRestApi() {
        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<>();
        //header中的token參數(shù)非必填,傳空也可以
        tokenPar.name("token").description("請求接口所需Token")
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build();
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(metaData())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zkhx.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
    }
    private ApiInfo metaData() {
        return new ApiInfoBuilder()
                .title("XX服務API文檔")
                .description("sun")
                .termsOfServiceUrl("")
                .version("1.0")
                .build();
    }
}

Docket(DocumentationType.SWAGGER_2)  :DocumentationType.SWAGGER_2 固定的,代swagger2
groupName("") // 如果配置多個文檔的時候,那么需要配置groupName來分組標識
apiInfo(apiInfo()) // 用于生成API信息
select() // select()函數(shù)返回一個ApiSelectorBuilder實例,用來控制接口被swagger做成文檔
apis(RequestHandlerSelectors.basePackage("com.example.controller")) // 用于指定掃描哪個包下的接口
paths(PathSelectors.any())// 選擇所有的API,如果你想只為部分API生成文檔,可以配置這里
apiInfo() :
    title("XX項目API") //  可以用來自定義API的主標題
     description("XX項目SwaggerAPI管理") // 可以用來描述整體的API
     termsOfServiceUrl("") // 用于定義服務的域名
      version("1.0") // 可以用來定義版本。

三級目錄

@Api(tags = “角色管理”) // tags:你可以當作是這個組的名字。

@RestController

public class RoleController {

}

@RestController
@Slf4j
@RequestMapping("/phm/db")
@Api(tags = "數(shù)據(jù)篩查")
public class DbController {
    @Autowired
    private StatusDbService statusDbService;
    @GetMapping("/field/data")
    @ApiOperation(value = "通過時間及單元id獲取數(shù)據(jù)篩查所需數(shù)據(jù)")
    public ResultMsg getFieldByUnitIdWithTime( 
    @ApiParam("結束時間") @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime);
    }

訪問

http://localhost:8080/doc.html

到此這篇關于Java詳解swagger2如何配置使用的文章就介紹到這了,更多相關Java swagger內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Springboot 接收POST、json、文本數(shù)據(jù)的方法 附示例

    Springboot 接收POST、json、文本數(shù)據(jù)的方法 附示例

    這篇文章主要介紹了Springboot 接收POST、json、文本數(shù)據(jù)實踐,如果把 json 作為參數(shù)傳遞,我們可以使用 @requestbody 接收參數(shù),將數(shù)據(jù)直接轉(zhuǎn)換成對象,本文通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-10-10
  • JVM中有哪些內(nèi)存區(qū)域及其作用

    JVM中有哪些內(nèi)存區(qū)域及其作用

    這篇文章主要介紹了JVM中有哪些內(nèi)存區(qū)域,分別是用來干什么的,vm內(nèi)又是如何劃分內(nèi)存的呢?這個內(nèi)被加載到了那一塊內(nèi)存中?,需要的朋友可以參考下
    2019-07-07
  • Elasticsearch倒排索引詳解及實際應用中的優(yōu)化

    Elasticsearch倒排索引詳解及實際應用中的優(yōu)化

    Elasticsearch(ES)使用倒排索引來加速文本的搜索速度,倒排索引之所以高效,主要是因為它改變了數(shù)據(jù)的組織方式,使得查詢操作可以快速完成,這篇文章主要給大家介紹了關于Elasticsearch倒排索引詳解及實際應用中優(yōu)化的相關資料,需要的朋友可以參考下
    2024-08-08
  • SpringBoot啟動應用及回調(diào)監(jiān)聽原理解析

    SpringBoot啟動應用及回調(diào)監(jiān)聽原理解析

    這篇文章主要介紹了SpringBoot啟動應用及回調(diào)監(jiān)聽原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • SpringBoot2.3集成ELK7.1.0的示例代碼

    SpringBoot2.3集成ELK7.1.0的示例代碼

    這篇文章主要介紹了SpringBoot2.3集成ELK7.1.0的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • 淺談spring中用到的設計模式及應用場景

    淺談spring中用到的設計模式及應用場景

    下面小編就為大家?guī)硪黄獪\談spring中用到的設計模式及應用場景。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • ZooKeeper官方文檔之Java客戶端開發(fā)案例翻譯

    ZooKeeper官方文檔之Java客戶端開發(fā)案例翻譯

    網(wǎng)上有很多ZooKeeper的java客戶端例子,我也看過很多,不過大部分寫的都不好,有各種問題。兜兜轉(zhuǎn)轉(zhuǎn)還是覺得官方給的例子最為經(jīng)典,在學習之余翻譯下來,供朋友們參考
    2022-01-01
  • 聊聊springboot?整合?hbase的問題

    聊聊springboot?整合?hbase的問題

    這篇文章主要介紹了springboot?整合?hbase的問題,文中給大家提到配置linux服務器hosts及配置window?hosts的相關知識,需要的朋友可以參考下
    2021-11-11
  • Eclipse中創(chuàng)建Web項目最新方法(2023年)

    Eclipse中創(chuàng)建Web項目最新方法(2023年)

    在Java開發(fā)人員中,最常用的開發(fā)工具應該就是Eclipse,下面這篇文章主要給大家介紹了關于Eclipse中創(chuàng)建Web項目2023年最新的方法,需要的朋友可以參考下
    2023-09-09
  • mybatis?log4j2打印sql+日志實例代碼

    mybatis?log4j2打印sql+日志實例代碼

    在學習mybatis的時候,如果用log4j2來協(xié)助查看調(diào)試信息,則會大大提高學習的效率,加快debug速度,下面這篇文章主要給大家介紹了關于mybatis?log4j2打印sql+日志的相關資料,需要的朋友可以參考下
    2022-08-08

最新評論