啥是倍增思想? 倍增,每次將範圍擴大或減少一倍而達到加速的效果 舉個慄子,你想要跳到15米遠的地方,你怎麼找到這個15這個地方,一步一步跳嗎,利用倍增的話 預設一個k使2^k>15值 ,這裡我們假設k=5, 2^5=32 >15 k- -; k=4; 跳過了,不跳 2^4=16 >15 k- -; ...
轉自:
http://www.java265.com/JavaCourse/202204/2931.html
SpringBoot:
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。通過這種方式,Spring Boot致力於在蓬勃發展的快速應用開發領域(rapid application development)成為領導者
在生產環境中,我們需關閉swagger配置,避免介面對外暴露而產生各種危險,具體的操作方式如下所示:
禁用方式1: 使用@Value
package com.java265; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 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 Swagger2Config extends WebMvcConfigurerAdapter { @Value("${swagger.enable}") private Boolean enable; @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .enable(enable) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.dc.controller")) .paths(PathSelectors.any()) //.paths(PathSelectors.none()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("auth系統數據介面文檔") .description("此系統為新架構Api說明文檔") .termsOfServiceUrl("") .contact(new Contact("[email protected]", "", "*********")) .version("1.0") .build(); } /** * swagger ui資源映射 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** * swagger-ui.html路徑映射,瀏覽器中使用/api-docs訪問 * @param registry */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("/api-docs","/swagger-ui.html"); } }
方式二:使用Profile註解
使用註解@Profile({“dev”,“test”})
在開發或測試環境開啟
package com.java265.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; 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 @Profile({“dev”,“test”}) public class Swagger2Config extends WebMvcConfigurerAdapter { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.dc.controller")) .paths(PathSelectors.any()) //.paths(PathSelectors.none()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("auth系統數據介面文檔") .description("此系統為新架構Api說明文檔") .termsOfServiceUrl("") .contact(new Contact("*****", "", "**")) .version("1.0") .build(); } /** * swagger ui資源映射 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } /** * swagger-ui.html路徑映射,瀏覽器中使用/api-docs訪問 * @param registry */ @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addRedirectViewController("/api-docs","/swagger-ui.html"); } }