diff --git a/pom.xml b/pom.xml index 3565c2dc4b..ffdfe4cffa 100644 --- a/pom.xml +++ b/pom.xml @@ -701,6 +701,7 @@ spring-rest-compress spring-rest-hal-browser spring-rest-http + spring-rest-http-2 spring-rest-query-language spring-rest-shell spring-rest-simple diff --git a/spring-rest-http-2/README.md b/spring-rest-http-2/README.md new file mode 100644 index 0000000000..74ec59e0b5 --- /dev/null +++ b/spring-rest-http-2/README.md @@ -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: + + diff --git a/spring-rest-http-2/pom.xml b/spring-rest-http-2/pom.xml new file mode 100644 index 0000000000..8678d7243d --- /dev/null +++ b/spring-rest-http-2/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + spring-rest-http-2 + 0.1-SNAPSHOT + spring-rest-http-2 + war + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + io.springfox + springfox-swagger2 + ${swagger2.version} + + + io.springfox + springfox-swagger-ui + ${swagger2.version} + + + + + 2.9.2 + + + diff --git a/spring-rest-http-2/src/main/java/com/baeldung/SpringBootRest2Application.java b/spring-rest-http-2/src/main/java/com/baeldung/SpringBootRest2Application.java new file mode 100644 index 0000000000..f8127f3686 --- /dev/null +++ b/spring-rest-http-2/src/main/java/com/baeldung/SpringBootRest2Application.java @@ -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); + } +} diff --git a/spring-rest-http-2/src/main/java/com/baeldung/swaggerui/disable/config/SwaggerConfig.java b/spring-rest-http-2/src/main/java/com/baeldung/swaggerui/disable/config/SwaggerConfig.java new file mode 100644 index 0000000000..8a9a921036 --- /dev/null +++ b/spring-rest-http-2/src/main/java/com/baeldung/swaggerui/disable/config/SwaggerConfig.java @@ -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/"); + } +} diff --git a/spring-rest-http-2/src/main/java/com/baeldung/swaggerui/disable/controllers/VersionController.java b/spring-rest-http-2/src/main/java/com/baeldung/swaggerui/disable/controllers/VersionController.java new file mode 100644 index 0000000000..8f8115197e --- /dev/null +++ b/spring-rest-http-2/src/main/java/com/baeldung/swaggerui/disable/controllers/VersionController.java @@ -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; + } + } +}