主要說明 Spring Boot 項目的集成html
0、其餘更多信息參考java
https://swagger.io/tools/swagger-ui/web
一、引入依賴spring
<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> |
二、添加2個配置類api
package com.moon.coupon.service.swagger.swagger2; import com.google.common.base.Predicate; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.bind.annotation.RequestMethod; import springfox.documentation.RequestHandler; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.builders.ResponseMessageBuilder; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.ResponseMessage; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList; import java.util.List; import java.util.Set; @Configuration @EnableSwagger2 public class Swagger2 { private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("獎勵券系統 接口清單") .description("紅包加息券拆分") .termsOfServiceUrl("https://localhost:8081/") .version("1.0.0") .build(); } @Bean public Docket createRestApi() { //能夠控制 哪些符合條件的 接口 對外暴露文檔; Predicate<RequestHandler> predicate = (input) -> { Set<String> patterns = input.getRequestMapping().getPatternsCondition().getPatterns(); for (String cur : patterns) { if (cur.startsWith("/api")) return true; } return false; }; ResponseMessage responseMesssageSucc = new ResponseMessageBuilder() .code(0) .message("處理成功") .build(); ResponseMessage responseMesssageFail = new ResponseMessageBuilder() .code(-1) .message("處理失敗") .build(); List<ResponseMessage> list = new ArrayList(); list.add(responseMesssageSucc); list.add(responseMesssageFail); return new Docket(DocumentationType.SWAGGER_2) .useDefaultResponseMessages(false) .globalResponseMessage(RequestMethod.POST, list) .apiInfo(apiInfo()) .select() .apis(predicate) .apis(RequestHandlerSelectors.basePackage("com.moon.coupon.service.controller")) .paths(PathSelectors.any()) .build(); } } |
package com.moon.coupon.service.swagger.swagger2; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration public class Swagger2MvcConfig extends WebMvcConfigurationSupport { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("/public/**").addResourceLocations("classpath:/public/"); registry.addResourceHandler("swagger-ui.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); } } |
三、訪問地址app
http://${host}:${port}/swagger-ui.html
四、大概模樣ide