問題描述
使用swagger生成接口文檔后,訪問http://localhost:8888/swagger-ui.html#/,顯示如下:
有些強迫症的我,感覺看起來很不舒服,結果百度了好久,找到解決方案,剛接觸spring boot對於很多api還不是很熟悉,先mark再說
代碼如下:
package com.course.config; import com.google.common.base.Predicates; 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.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .pathMapping("/") .select() // 選擇那些路徑和api會生成document .apis(RequestHandlerSelectors.any())// 對所有api進行監控 //不顯示錯誤的接口地址 .paths(Predicates.not(PathSelectors.regex("/error.*")))//錯誤路徑不監控 .paths(PathSelectors.regex("/.*"))// 對根下所有路徑進行監控 .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder().title("這是我的接口文檔") .contact(new Contact("rongrong", "", "emai@qq.com")) .description("這是SWAGGER_2生成的接口文檔") .termsOfServiceUrl("NO terms of service") .license("The Apache License, Version 2.0") .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html") .version("v1.0") .build(); } }
重啟服務:再次訪問如下