From 5fef9e01bb628287dd4583cd6fd3e598ce6a50f6 Mon Sep 17 00:00:00 2001 From: Gaetano Piazzolla Date: Thu, 3 Feb 2022 11:17:55 +0100 Subject: [PATCH] BAEL-5319 - Remove Basic Error Controller in Swagger (#11743) * BAEL-5319 - Remove Basic Error Controller in Swagger * BAEL-5319 - fixed style * BAEL-5319 - removed comments and private info * BAEL-5319 - removed private info * two space indent for line continuation, optimized import. Co-authored-by: gpiazzolla --- .../SpringBootSwaggerConfApplication.java | 13 +++++++ .../configuration/SwaggerConfiguration.java | 37 +++++++++++++++++++ .../ErrorControllerExcludedForAnnotation.java | 17 +++++++++ .../ErrorControllerExcludedForApiIgnore.java | 21 +++++++++++ .../ErrorControllerExcludedForPath.java | 19 ++++++++++ .../controller/RegularRestController.java | 33 +++++++++++++++++ .../ErrorControllerExcludedForPackage.java | 17 +++++++++ .../src/main/resources/application.properties | 1 + ...ExcludeErrorControllerIntegrationTest.java | 33 +++++++++++++++++ 9 files changed, 191 insertions(+) create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/SpringBootSwaggerConfApplication.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/configuration/SwaggerConfiguration.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForAnnotation.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForApiIgnore.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForPath.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/RegularRestController.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/excluded/ErrorControllerExcludedForPackage.java create mode 100644 spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerconf/SwaggerConfExcludeErrorControllerIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/SpringBootSwaggerConfApplication.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/SpringBootSwaggerConfApplication.java new file mode 100644 index 0000000000..1d2e129f26 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/SpringBootSwaggerConfApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.swaggerconf; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootSwaggerConfApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootSwaggerConfApplication.class, args); + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/configuration/SwaggerConfiguration.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/configuration/SwaggerConfiguration.java new file mode 100644 index 0000000000..a78335a2fe --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/configuration/SwaggerConfiguration.java @@ -0,0 +1,37 @@ +package com.baeldung.swaggerconf.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.bind.annotation.RestController; +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; + +import java.util.Collections; + +import static springfox.documentation.builders.PathSelectors.regex; + +@Configuration +@EnableSwagger2 +public class SwaggerConfiguration { + + private ApiInfo apiInfo() { + return new ApiInfo("My REST API", "Some custom description of API.", "API TOS", "Terms of service", + new Contact("General UserName", "www.baeldung.com", "user-name@gmail.com"), + "License of API", "API license URL", Collections.emptyList()); + } + + @Bean + public Docket api() { + return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) + .select() + .apis(RequestHandlerSelectors.basePackage("com.baeldung.swaggerconf.controller")) + .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)) + .paths(regex("/good-path/.*")) + .build(); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForAnnotation.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForAnnotation.java new file mode 100644 index 0000000000..65059bf446 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForAnnotation.java @@ -0,0 +1,17 @@ +package com.baeldung.swaggerconf.controller; + +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; + +@Component +@RequestMapping("good-path/error-excluded-annotation") +public class ErrorControllerExcludedForAnnotation extends BasicErrorController { + + public ErrorControllerExcludedForAnnotation(ErrorAttributes errorAttributes, ServerProperties serverProperties) { + super(errorAttributes, serverProperties.getError()); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForApiIgnore.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForApiIgnore.java new file mode 100644 index 0000000000..88208d8446 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForApiIgnore.java @@ -0,0 +1,21 @@ +package com.baeldung.swaggerconf.controller; + +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import springfox.documentation.annotations.ApiIgnore; + +@Component +@RequestMapping("good-path/error-excluded-apiignore") +@RestController +@ApiIgnore +public class ErrorControllerExcludedForApiIgnore extends BasicErrorController { + + public ErrorControllerExcludedForApiIgnore(ErrorAttributes errorAttributes, ServerProperties serverProperties) { + super(errorAttributes, serverProperties.getError()); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForPath.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForPath.java new file mode 100644 index 0000000000..5cea4f5ef9 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/ErrorControllerExcludedForPath.java @@ -0,0 +1,19 @@ +package com.baeldung.swaggerconf.controller; + +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@Component +@RequestMapping("wrong-path/error-excluded-path") +@RestController +public class ErrorControllerExcludedForPath extends BasicErrorController { + + public ErrorControllerExcludedForPath(ErrorAttributes errorAttributes, ServerProperties serverProperties) { + super(errorAttributes, serverProperties.getError()); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/RegularRestController.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/RegularRestController.java new file mode 100644 index 0000000000..163826e626 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/controller/RegularRestController.java @@ -0,0 +1,33 @@ +package com.baeldung.swaggerconf.controller; + +import io.swagger.annotations.ApiOperation; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.time.LocalDate; +import java.time.LocalTime; + +@RestController +@RequestMapping("good-path") +public class RegularRestController { + + @ApiOperation(value = "This method is used to get the author name.") + @GetMapping("/getAuthor") + public String getAuthor() { + return "Name Surname"; + } + + @ApiOperation(value = "This method is used to get the current date.") + @GetMapping("/getDate") + public LocalDate getDate() { + return LocalDate.now(); + } + + @ApiOperation(value = "This method is used to get the current time.") + @GetMapping("/getTime") + public LocalTime getTime() { + return LocalTime.now(); + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/excluded/ErrorControllerExcludedForPackage.java b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/excluded/ErrorControllerExcludedForPackage.java new file mode 100644 index 0000000000..a3b5a3fe63 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/main/java/com/baeldung/swaggerconf/excluded/ErrorControllerExcludedForPackage.java @@ -0,0 +1,17 @@ +package com.baeldung.swaggerconf.excluded; + +import org.springframework.boot.autoconfigure.web.ServerProperties; +import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; +import org.springframework.boot.web.servlet.error.ErrorAttributes; +import org.springframework.stereotype.Component; +import org.springframework.web.bind.annotation.RequestMapping; + +@Component +@RequestMapping("good-path/error-excluded-package") +public class ErrorControllerExcludedForPackage extends BasicErrorController { + + public ErrorControllerExcludedForPackage(ErrorAttributes errorAttributes, ServerProperties serverProperties) { + super(errorAttributes, serverProperties.getError()); + } + +} diff --git a/spring-boot-modules/spring-boot-swagger/src/main/resources/application.properties b/spring-boot-modules/spring-boot-swagger/src/main/resources/application.properties index e69de29bb2..a4e98652bc 100644 --- a/spring-boot-modules/spring-boot-swagger/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-swagger/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerconf/SwaggerConfExcludeErrorControllerIntegrationTest.java b/spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerconf/SwaggerConfExcludeErrorControllerIntegrationTest.java new file mode 100644 index 0000000000..c0a5c54d69 --- /dev/null +++ b/spring-boot-modules/spring-boot-swagger/src/test/java/com/baeldung/swaggerconf/SwaggerConfExcludeErrorControllerIntegrationTest.java @@ -0,0 +1,33 @@ +package com.baeldung.swaggerconf; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.ResultActions; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@AutoConfigureMockMvc +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class SwaggerConfExcludeErrorControllerIntegrationTest { + + @Autowired + private MockMvc mvc; + + @Test + public void whenCallingSwaggerJSON_stringObjectDoesNotContainAnyErrorControllers() throws Exception { + ResultActions resultActions = mvc.perform(get("/v2/api-docs")).andExpect(status().isOk()); + MvcResult result = resultActions.andReturn(); + String content = result.getResponse().getContentAsString(); + Assert.assertNotNull(content); + Assert.assertFalse(content.contains("error-controller")); + } +} \ No newline at end of file