SpringBoot集成Swagger3.0
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
66套java從入門(mén)到精通實(shí)戰(zhàn)課程分享?
1、pom.xml
??io.springfox
??springfox-boot-starter
??3.0.0
2、SwaggerConfig
/**
?*?@author:?zek
?*?@desc:?swagger
?*/
@EnableOpenApi
@Configuration
public?class?SwaggerConfig?implements?WebMvcConfigurer?{
??@Bean
??public?Docket?createRestApi()?{
????return?new?Docket(DocumentationType.OAS_30)
????????.pathMapping("/")
????????//?定義是否開(kāi)啟swagger,false為關(guān)閉,可以通過(guò)變量控制
????????.enable(true)
????????//?將api的元信息設(shè)置為包含在json ResourceListing響應(yīng)中。
????????.apiInfo(apiInfo())
????????//?接口調(diào)試地址
????????.host("http://localhost:8080")
????????//?選擇哪些接口作為swagger的doc發(fā)布
????????.select()
????????.apis(RequestHandlerSelectors.basePackage("xx.xx.xx.controller"))
????????.paths(PathSelectors.any())
????????.build()
????????//?支持的通訊協(xié)議集合
????????.protocols(Sets.newHashSet("http",?"https"))
????????//?授權(quán)信息設(shè)置,必要的header?token等認(rèn)證信息
????????.securitySchemes(securitySchemes())
????????//?授權(quán)信息全局應(yīng)用
????????.securityContexts(securityContexts());
??}
??/**?API?頁(yè)面上半部分展示信息?*/
??private?ApiInfo?apiInfo()?{
????return?new?ApiInfoBuilder()
????????.title("?Api?Doc")
????????.description("SpringFox?3.0.0?發(fā)布:?https://xx.xx.xx/xx/swagger-ui/index.html")
????????.contact(new?Contact("lighter",?null,?"[email protected]"))
????????.version(
????????????"Application?Version:?"
????????????????+?"1.0.0"
????????????????+?",?Spring?Boot?Version:?"
????????????????+?SpringBootVersion.getVersion())
????????.build();
??}
??/**?設(shè)置授權(quán)信息?*/
??private?List?securitySchemes()?{
????ApiKey?apiKey?=?new?ApiKey("BASE_TOKEN",?"token",?In.HEADER.toValue());
????return?Collections.singletonList(apiKey);
??}
??/**?授權(quán)信息全局應(yīng)用?*/
??private?List?securityContexts()?{
????return?Collections.singletonList(
????????SecurityContext.builder()
????????????.securityReferences(
????????????????Collections.singletonList(
????????????????????new?SecurityReference(
????????????????????????"BASE_TOKEN",
????????????????????????new?AuthorizationScope[]?{new?AuthorizationScope("global",?"")})))
????????????.build());
??}
??/**?通用攔截器排除swagger設(shè)置,所有攔截器都會(huì)自動(dòng)加swagger相關(guān)的資源排除信息?*/
??@SuppressWarnings("unchecked")
??@Override
??public?void?addInterceptors(InterceptorRegistry?registry)?{
????try?{
??????Field?registrationsField?=
??????????FieldUtils.getField(InterceptorRegistry.class,?"registrations",?true);
??????List?registrations?=
??????????(List)?ReflectionUtils.getField(registrationsField,?registry);
??????if?(registrations?!=?null)?{
????????for?(InterceptorRegistration?interceptorRegistration?:?registrations)?{
??????????interceptorRegistration
??????????????.excludePathPatterns("/swagger**/**")
??????????????.excludePathPatterns("/webjars/**")
??????????????.excludePathPatterns("/v3/**")
??????????????.excludePathPatterns("/doc.html");
????????}
??????}
????}?catch?(Exception?e)?{
??????e.printStackTrace();
????}
??}
}
3、使用
entity中的@ApiModel注解中的參數(shù)加了和沒(méi)加一個(gè)效果,所以,直接寫(xiě)@ApiModel就可以了。
@ApiParam(value = “文章ID”) 注解中的value參數(shù)只有和:@PathVariable(value = “id”) 以及@RequestParam使用,value參數(shù)才會(huì)有效果。其他沒(méi)有
? ? 1.controller層使用
@Api(tags?=?{"1.?文章"})
@RestController
@RequestMapping("/article")
public?class?ArticleController?{}
? ? 2.接口使用
@ApiOperation(value?=?"文章列表")
@GetMapping("/article")
public?Result? ? 3.Model使用
@ApiModel(value?=?"ArticleListReq文章列表請(qǐng)求實(shí)體")
@Data
public?class?ArticleListReq?{
??@ApiModelProperty(value?=?"pageNum")
??private?Integer?pageNum;
??@ApiModelProperty(value?=?"pageSize")
??private?Integer?pageSize;
??@ApiModelProperty(value?=?"關(guān)鍵字搜索:標(biāo)題、描述、內(nèi)容")
??private?String?keyword;
??@ApiModelProperty(value?=?"資源類(lèi)型")
??private?Integer?type;
}
? ? 4.忽略接口
@ApiIgnore
? ?5.Swagger-ui URL
https://ip:port或域名/設(shè)置的請(qǐng)求前綴/swagger-ui/index.html#/
? ?6.base-error-controller
SwaggerConfig新增指定包
.apis(RequestHandlerSelectors.basePackage("xx.xx.xx.controller"))
4、查看頁(yè)面
5、發(fā)現(xiàn)部分注解參數(shù)使用了,并沒(méi)有效果,
版權(quán)聲明:本文為博主原創(chuàng)文章,遵循 CC 4.0 BY-SA 版權(quán)協(xié)議,轉(zhuǎn)載請(qǐng)附上原文出處鏈接和本聲明。
本文鏈接:
https://blog.csdn.net/qq_17213067/article/details/111573316
粉絲福利:Java從入門(mén)到入土學(xué)習(xí)路線圖
???

?長(zhǎng)按上方微信二維碼?2 秒
感謝點(diǎn)贊支持下哈?
評(píng)論
圖片
表情
