swagger-bootstrap-ui是基於swagger-ui做了一些優化拓展:
swagger-ui的界面:
swagger-bootstrap-ui界面:
相比於原生的swagger-ui ,swagger-bootstarp-ui提供了更好的ui界面,以及入參,出參直觀的分層;下面將swagge-bootstrap-ui整合到springboot項目中:
//springboot版本
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<!--swagger相關依賴--> <!--原生swagger-ui--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-bean-validators</artifactId> <version>2.9.2</version> </dependency> <!--增強swagger-bootstrap-ui--> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.3</version> </dependency> <!-- # 增加兩個配置解決 NumberFormatException --> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.22</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-models</artifactId> <version>1.5.22</version> </dependency>
@Slf4j @Configuration @EnableSwagger2 @EnableSwaggerBootstrapUI public class Swagger2Config implements WebMvcConfigurer { /** * 顯示swagger-ui.html文檔展示頁,還必須注入swagger資源: * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** * 幫助中心 (不同的模塊這里分不同的包掃描basePackage) * Docket 可以配置多個 * * @return */ @Bean public Docket assist() { return new Docket(DocumentationType.SWAGGER_2) .globalOperationParameters(setRequestHeaders()) //.apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.xxxx.modules.assist.controller")) //加了ApiOperation注解的類,才生成接口文檔 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build() .groupName("幫助中心"); } /** * 設置請求頭 * * @return */ private List<Parameter> setRequestHeaders() { ParameterBuilder ticketPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<Parameter>(); ticketPar.name("token").description("用戶token") .modelRef(new ModelRef("string")).parameterType("header") .required(false).build(); //header中的ticket參數非必填,傳空也可以 pars.add(ticketPar.build()); //根據每個方法名也知道當前方法在設置什么參數 return pars; } }
建議在啟動類中,直接將swagge-ui文檔記錄下來
public static void main(String[] args) throws Exception { ConfigurableApplicationContext application = SpringApplication.run(StageApplication.class, args); Environment env = application.getEnvironment(); String ip = InetAddress.getLocalHost().getHostAddress(); String port = env.getProperty("server.port"); //String path = env.getProperty("server.servlet.context-path"); String active = env.getProperty("spring.profiles.active"); String maxFileSize = env.getProperty("spring.servlet.multipart.max-file-size"); //最大文件大小 String maxRequestSize = env.getProperty("spring.servlet.multipart.max-request-size"); //最大請求大小 log.info("\n----------------------------------------------------------\n\t" + "Application is running! Access URLs:\n\t" + "Doc: \t\thttp://" + ip + ":" + port + "/doc.html\n\t" + "spring-profiles-active: \t\t" + active + "\n\t" + "max-file-size: \t\t" + maxFileSize + "\n\t" + "max-request-size: \t\t" + maxRequestSize + "\n" + "----------------------------------------------------------"); }