Last active
October 12, 2023 08:36
-
-
Save Christian-Oette/137dc74630262226e381d740c9ab1000 to your computer and use it in GitHub Desktop.
Override BasicErrorController in spring boot to disable whitelabel page and to remove the default error return
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Map; | |
import jakarta.servlet.RequestDispatcher; | |
import jakarta.servlet.http.HttpServletRequest; | |
import org.springframework.boot.web.servlet.error.ErrorController; | |
import org.springframework.http.HttpStatus; | |
import org.springframework.http.MediaType; | |
import org.springframework.http.ResponseEntity; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.web.bind.annotation.RequestMapping; | |
/** | |
* Hide spring boot internals and exceptions which the client should not see. | |
* | |
* Example: | |
* CURL/Postman to unknown path -> Empty body via error() with 404 status | |
* Browser call to unknown path -> Empty body via errorHtml() with 404 status | |
*/ | |
@Controller | |
@RequestMapping("${server.error.path:${error.path:/error}}") | |
public class CustomErrorController implements ErrorController { | |
/** | |
* Disables default spring error resource with list of attributes | |
*/ | |
@RequestMapping | |
public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) { | |
HttpStatus status = getStatus(request); | |
return new ResponseEntity<>(status); | |
} | |
/** | |
* Disables whitelist label page | |
*/ | |
@RequestMapping(produces = MediaType.TEXT_HTML_VALUE) | |
public ResponseEntity<Map<String, Object>> errorHtml(HttpServletRequest request) { | |
HttpStatus status = getStatus(request); | |
return new ResponseEntity<>(status); | |
} | |
protected HttpStatus getStatus(HttpServletRequest request) { | |
Integer statusCode = (Integer) request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); | |
if (statusCode == null) { | |
return HttpStatus.INTERNAL_SERVER_ERROR; | |
} | |
try { | |
return HttpStatus.valueOf(statusCode); | |
} | |
catch (Exception ex) { | |
return HttpStatus.INTERNAL_SERVER_ERROR; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | |
import org.hamcrest.Matchers; | |
import org.junit.jupiter.api.Test; | |
import org.junit.jupiter.api.extension.ExtendWith; | |
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.http.MediaType; | |
import org.springframework.test.context.junit.jupiter.SpringExtension; | |
import org.springframework.test.web.servlet.MockMvc; | |
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | |
@ExtendWith(SpringExtension.class) | |
@SpringBootTest | |
@AutoConfigureMockMvc | |
class CustomErrorControllerITest { | |
@Autowired | |
private MockMvc mockMvc; | |
@Test | |
void getRequestWithunmappedPathForJSONReturnsCorrectly404WithEmptyBody() throws Exception { | |
mockMvc.perform( | |
get("/not-mapped-path").accept(MediaType.APPLICATION_JSON) | |
) | |
.andExpect(MockMvcResultMatchers.status().isNotFound()) | |
.andExpect(MockMvcResultMatchers.content().string(Matchers.blankString())); | |
} | |
@Test | |
void getRequestWithUnmappedPathForHTMLReturnsCorrectly404WithEmptyBody() throws Exception { | |
mockMvc.perform( | |
get("/not-mapped-path").accept(MediaType.TEXT_HTML) | |
) | |
.andExpect(MockMvcResultMatchers.status().isNotFound()) | |
.andExpect(MockMvcResultMatchers.content().string(Matchers.blankString())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment