BAEL-4172 - How to Turn Off Swagger-ui in Production in http-2 (#9858)
* How to disable swagger in production example. * Add new module in main pom. * fix pom formatting * Replace tabs with spaces. * Move to article-specific package. Co-authored-by: Cristian Stancalau <cstancalau@consultant.ffn.com>
This commit is contained in:
parent
93587c1ee5
commit
bbeb322d41
1
pom.xml
1
pom.xml
|
@ -701,6 +701,7 @@
|
|||
<module>spring-rest-compress</module>
|
||||
<module>spring-rest-hal-browser</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-rest-http-2</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest-shell</module>
|
||||
<module>spring-rest-simple</module>
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
## Spring REST HTTP 2
|
||||
|
||||
This module contains articles about HTTP in REST APIs with Spring
|
||||
|
||||
### The Course
|
||||
The "REST With Spring 2" Classes: http://bit.ly/restwithspring
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>spring-rest-http-2</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>spring-rest-http-2</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger2</artifactId>
|
||||
<version>${swagger2.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.springfox</groupId>
|
||||
<artifactId>springfox-swagger-ui</artifactId>
|
||||
<version>${swagger2.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<swagger2.version>2.9.2</swagger2.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SpringBootRest2Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringBootRest2Application.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.swaggerui.disable.config;
|
||||
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
import springfox.documentation.builders.PathSelectors;
|
||||
import springfox.documentation.builders.RequestHandlerSelectors;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
import springfox.documentation.swagger2.annotations.EnableSwagger2;
|
||||
|
||||
@Profile("!prod")
|
||||
// @Profile("swagger")
|
||||
// @Profile("!prod && swagger")
|
||||
// @ConditionalOnExpression(value = "${useSwagger:false}")
|
||||
@Configuration
|
||||
@EnableSwagger2
|
||||
public class SwaggerConfig implements WebMvcConfigurer {
|
||||
|
||||
@Bean
|
||||
public Docket api() {
|
||||
return new Docket(DocumentationType.SWAGGER_2).select()
|
||||
.apis(RequestHandlerSelectors.basePackage("com.baeldung"))
|
||||
.paths(PathSelectors.regex("/.*"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@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/");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.swaggerui.disable.controllers;
|
||||
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class VersionController {
|
||||
|
||||
private final Environment environment;
|
||||
|
||||
public VersionController(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
@ApiOperation(value = "Get the currently deployed API version and active Spring profiles")
|
||||
@GetMapping("/api/version")
|
||||
public Version getVersion() {
|
||||
return new Version("1.0", environment.getActiveProfiles());
|
||||
}
|
||||
|
||||
private static class Version {
|
||||
private final String version;
|
||||
private String[] activeProfiles;
|
||||
|
||||
private Version(String version, String[] activeProfiles) {
|
||||
this.version = version;
|
||||
this.activeProfiles = activeProfiles;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public String[] getActiveProfiles() {
|
||||
return activeProfiles;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue