Java詳解swagger2如何配置使用
swagger可能會(huì)遇到的問(wèn)題:
當(dāng)我們的接口返回的數(shù)據(jù)太大的時(shí)候,swagger會(huì)崩潰
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開關(guān)
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 // 標(biāo)明是配置類 @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("請(qǐng)求接口所需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服務(wù)API文檔") .description("sun") .termsOfServiceUrl("") .version("1.0") .build(); } }
Docket(DocumentationType.SWAGGER_2) :DocumentationType.SWAGGER_2 固定的,代swagger2
groupName("") // 如果配置多個(gè)文檔的時(shí)候,那么需要配置groupName來(lái)分組標(biāo)識(shí)
apiInfo(apiInfo()) // 用于生成API信息
select() // select()函數(shù)返回一個(gè)ApiSelectorBuilder實(shí)例,用來(lái)控制接口被swagger做成文檔
apis(RequestHandlerSelectors.basePackage("com.example.controller")) // 用于指定掃描哪個(gè)包下的接口
paths(PathSelectors.any())// 選擇所有的API,如果你想只為部分API生成文檔,可以配置這里
apiInfo() :
title("XX項(xiàng)目API") // 可以用來(lái)自定義API的主標(biāo)題
description("XX項(xiàng)目SwaggerAPI管理") // 可以用來(lái)描述整體的API
termsOfServiceUrl("") // 用于定義服務(wù)的域名
version("1.0") // 可以用來(lái)定義版本。
三級(jí)目錄
@Api(tags = “角色管理”) // tags:你可以當(dāng)作是這個(gè)組的名字。
@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 = "通過(guò)時(shí)間及單元id獲取數(shù)據(jù)篩查所需數(shù)據(jù)") public ResultMsg getFieldByUnitIdWithTime( @ApiParam("結(jié)束時(shí)間") @RequestParam(value = "endTime", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime); }
訪問(wèn)
http://localhost:8080/doc.html
到此這篇關(guān)于Java詳解swagger2如何配置使用的文章就介紹到這了,更多相關(guān)Java swagger內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot 接收POST、json、文本數(shù)據(jù)的方法 附示例
這篇文章主要介紹了Springboot 接收POST、json、文本數(shù)據(jù)實(shí)踐,如果把 json 作為參數(shù)傳遞,我們可以使用 @requestbody 接收參數(shù),將數(shù)據(jù)直接轉(zhuǎn)換成對(duì)象,本文通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10Elasticsearch倒排索引詳解及實(shí)際應(yīng)用中的優(yōu)化
Elasticsearch(ES)使用倒排索引來(lái)加速文本的搜索速度,倒排索引之所以高效,主要是因?yàn)樗淖兞藬?shù)據(jù)的組織方式,使得查詢操作可以快速完成,這篇文章主要給大家介紹了關(guān)于Elasticsearch倒排索引詳解及實(shí)際應(yīng)用中優(yōu)化的相關(guān)資料,需要的朋友可以參考下2024-08-08SpringBoot啟動(dòng)應(yīng)用及回調(diào)監(jiān)聽原理解析
這篇文章主要介紹了SpringBoot啟動(dòng)應(yīng)用及回調(diào)監(jiān)聽原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12淺談spring中用到的設(shè)計(jì)模式及應(yīng)用場(chǎng)景
下面小編就為大家?guī)?lái)一篇淺談spring中用到的設(shè)計(jì)模式及應(yīng)用場(chǎng)景。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08ZooKeeper官方文檔之Java客戶端開發(fā)案例翻譯
網(wǎng)上有很多ZooKeeper的java客戶端例子,我也看過(guò)很多,不過(guò)大部分寫的都不好,有各種問(wèn)題。兜兜轉(zhuǎn)轉(zhuǎn)還是覺(jué)得官方給的例子最為經(jīng)典,在學(xué)習(xí)之余翻譯下來(lái),供朋友們參考2022-01-01Eclipse中創(chuàng)建Web項(xiàng)目最新方法(2023年)
在Java開發(fā)人員中,最常用的開發(fā)工具應(yīng)該就是Eclipse,下面這篇文章主要給大家介紹了關(guān)于Eclipse中創(chuàng)建Web項(xiàng)目2023年最新的方法,需要的朋友可以參考下2023-09-09mybatis?log4j2打印sql+日志實(shí)例代碼
在學(xué)習(xí)mybatis的時(shí)候,如果用log4j2來(lái)協(xié)助查看調(diào)試信息,則會(huì)大大提高學(xué)習(xí)的效率,加快debug速度,下面這篇文章主要給大家介紹了關(guān)于mybatis?log4j2打印sql+日志的相關(guān)資料,需要的朋友可以參考下2022-08-08