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 <gaetano.piazzolla@dxc.com>
This commit is contained in:
Gaetano Piazzolla 2022-02-03 11:17:55 +01:00 committed by GitHub
parent 9b8a5a0fa7
commit 533f2014a8
9 changed files with 191 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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();
}
}

View File

@ -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());
}
}

View File

@ -0,0 +1 @@
spring.mvc.pathmatch.matching-strategy = ANT_PATH_MATCHER

View File

@ -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"));
}
}