43.3 Override Spring default white label error - Way 3 - Error Endpoint (#3)
This commit is contained in:
@ -0,0 +1,18 @@
|
||||
package net.shyshkin.study.fullstack.supportportal.backend.controller;
|
||||
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static net.shyshkin.study.fullstack.supportportal.backend.utility.HttpResponseUtility.createHttpResponse;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
@RestController
|
||||
public class ErrorController {
|
||||
|
||||
@GetMapping("/error")
|
||||
public ResponseEntity<HttpResponse> error() {
|
||||
return createHttpResponse(NOT_FOUND, "Resource not found");
|
||||
}
|
||||
}
|
||||
@ -8,7 +8,6 @@ import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.Email
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.UserNotFoundException;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.exception.domain.UsernameExistsException;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.access.AccessDeniedException;
|
||||
import org.springframework.security.authentication.BadCredentialsException;
|
||||
@ -17,12 +16,12 @@ import org.springframework.security.authentication.LockedException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
|
||||
import javax.persistence.NoResultException;
|
||||
import java.io.IOException;
|
||||
import java.util.Objects;
|
||||
|
||||
import static net.shyshkin.study.fullstack.supportportal.backend.utility.HttpResponseUtility.createHttpResponse;
|
||||
import static org.springframework.http.HttpStatus.*;
|
||||
|
||||
@Slf4j
|
||||
@ -75,10 +74,10 @@ public class ExceptionHandling {
|
||||
return createHttpResponse(METHOD_NOT_ALLOWED, String.format(METHOD_IS_NOT_ALLOWED, supportedMethod));
|
||||
}
|
||||
|
||||
@ExceptionHandler(NoHandlerFoundException.class)
|
||||
public ResponseEntity<HttpResponse> noHandlerFoundException(NoHandlerFoundException exception) {
|
||||
return createHttpResponse(BAD_REQUEST, "This page was not found");
|
||||
}
|
||||
// @ExceptionHandler(NoHandlerFoundException.class)
|
||||
// public ResponseEntity<HttpResponse> noHandlerFoundException(NoHandlerFoundException exception) {
|
||||
// return createHttpResponse(BAD_REQUEST, "This page was not found");
|
||||
// }
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<HttpResponse> internalServerErrorException(Exception exception) {
|
||||
@ -97,14 +96,4 @@ public class ExceptionHandling {
|
||||
log.error(exception.getMessage());
|
||||
return createHttpResponse(INTERNAL_SERVER_ERROR, ERROR_PROCESSING_FILE);
|
||||
}
|
||||
|
||||
private ResponseEntity<HttpResponse> createHttpResponse(HttpStatus httpStatus, String message) {
|
||||
HttpResponse httpResponse = HttpResponse.builder()
|
||||
.httpStatus(httpStatus)
|
||||
.httpStatusCode(httpStatus.value())
|
||||
.reason(httpStatus.getReasonPhrase().toUpperCase())
|
||||
.message(message.toUpperCase())
|
||||
.build();
|
||||
return new ResponseEntity<>(httpResponse, httpStatus);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,19 @@
|
||||
package net.shyshkin.study.fullstack.supportportal.backend.utility;
|
||||
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
public class HttpResponseUtility {
|
||||
|
||||
public static ResponseEntity<HttpResponse> createHttpResponse(HttpStatus httpStatus, String message) {
|
||||
HttpResponse httpResponse = HttpResponse.builder()
|
||||
.httpStatus(httpStatus)
|
||||
.httpStatusCode(httpStatus.value())
|
||||
.reason(httpStatus.getReasonPhrase().toUpperCase())
|
||||
.message(message.toUpperCase())
|
||||
.build();
|
||||
return new ResponseEntity<>(httpResponse, httpStatus);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,7 +1,8 @@
|
||||
server:
|
||||
error:
|
||||
whitelabel:
|
||||
enabled: false
|
||||
path: /error
|
||||
# whitelabel:
|
||||
# enabled: false
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
|
||||
@ -0,0 +1,51 @@
|
||||
package net.shyshkin.study.fullstack.supportportal.backend.controller;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.shyshkin.study.fullstack.supportportal.backend.domain.HttpResponse;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.TestPropertySource;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.within;
|
||||
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||
import static org.springframework.http.HttpStatus.NOT_FOUND;
|
||||
|
||||
@Slf4j
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
|
||||
@TestPropertySource(properties = {
|
||||
"app.public-urls=**"
|
||||
})
|
||||
@ActiveProfiles("local")
|
||||
class ErrorControllerTest {
|
||||
|
||||
@Autowired
|
||||
TestRestTemplate restTemplate;
|
||||
|
||||
@Test
|
||||
void errorTest() {
|
||||
|
||||
//when
|
||||
var responseEntity = restTemplate.getForEntity("/absent/endpoint", HttpResponse.class);
|
||||
|
||||
//then
|
||||
log.debug("Response entity: {}", responseEntity);
|
||||
assertThat(responseEntity.getStatusCode()).isEqualTo(NOT_FOUND);
|
||||
assertThat(responseEntity.getBody())
|
||||
.isNotNull()
|
||||
.hasNoNullFieldsOrProperties()
|
||||
.satisfies(httpResponse -> assertAll(
|
||||
() -> assertThat(httpResponse.getHttpStatus()).isEqualTo(NOT_FOUND),
|
||||
() -> assertThat(httpResponse.getHttpStatusCode()).isEqualTo(NOT_FOUND.value()),
|
||||
() -> assertThat(httpResponse.getMessage()).isEqualTo("RESOURCE NOT FOUND"),
|
||||
() -> assertThat(httpResponse.getReason()).isEqualTo(NOT_FOUND.getReasonPhrase().toUpperCase()),
|
||||
() -> assertThat(httpResponse.getTimestamp()).isCloseTo(LocalDateTime.now(), within(200, ChronoUnit.MILLIS))
|
||||
));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user