minor fix
This commit is contained in:
@@ -3,6 +3,9 @@ package org.baeldung.web;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
|
||||
import org.springframework.beans.TypeMismatchException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -67,16 +70,6 @@ public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
|
||||
}
|
||||
|
||||
@ExceptionHandler({ MethodArgumentTypeMismatchException.class })
|
||||
public ResponseEntity<Object> handleMethodArgumentTypeMismatch(final MethodArgumentTypeMismatchException ex, final WebRequest request) {
|
||||
logger.info(ex.getClass().getName());
|
||||
//
|
||||
final String error = ex.getName() + " should be of type " + ex.getRequiredType().getName();
|
||||
|
||||
final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), error);
|
||||
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ResponseEntity<Object> handleMissingServletRequestPart(final MissingServletRequestPartException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
|
||||
logger.info(ex.getClass().getName());
|
||||
@@ -95,6 +88,32 @@ public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@ExceptionHandler({ MethodArgumentTypeMismatchException.class })
|
||||
public ResponseEntity<Object> handleMethodArgumentTypeMismatch(final MethodArgumentTypeMismatchException ex, final WebRequest request) {
|
||||
logger.info(ex.getClass().getName());
|
||||
//
|
||||
final String error = ex.getName() + " should be of type " + ex.getRequiredType().getName();
|
||||
|
||||
final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), error);
|
||||
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
|
||||
}
|
||||
|
||||
|
||||
@ExceptionHandler({ ConstraintViolationException.class })
|
||||
public ResponseEntity<Object> handleConstraintViolation(final ConstraintViolationException ex, final WebRequest request) {
|
||||
logger.info(ex.getClass().getName());
|
||||
//
|
||||
final List<String> errors = new ArrayList<String>();
|
||||
for (final ConstraintViolation<?> violation : ex.getConstraintViolations()) {
|
||||
errors.add(violation.getRootBeanClass().getName() + " " + violation.getPropertyPath() + ": " + violation.getMessage());
|
||||
}
|
||||
|
||||
final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
|
||||
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
|
||||
}
|
||||
|
||||
// 404
|
||||
|
||||
@Override
|
||||
@@ -116,7 +135,7 @@ public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append(ex.getMethod());
|
||||
builder.append(" method is not supported for this request. Supported methods are ");
|
||||
ex.getSupportedMethods();
|
||||
ex.getSupportedHttpMethods().forEach(t -> builder.append(t + " "));
|
||||
|
||||
final ApiError apiError = new ApiError(HttpStatus.METHOD_NOT_ALLOWED, ex.getLocalizedMessage(), builder.toString());
|
||||
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
|
||||
@@ -131,7 +150,7 @@ public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
builder.append(ex.getContentType());
|
||||
builder.append(" media type is not supported. Supported media types are ");
|
||||
ex.getSupportedMediaTypes().forEach(t -> builder.append(t + ", "));
|
||||
ex.getSupportedMediaTypes().forEach(t -> builder.append(t + " "));
|
||||
|
||||
final ApiError apiError = new ApiError(HttpStatus.UNSUPPORTED_MEDIA_TYPE, ex.getLocalizedMessage(), builder.substring(0, builder.length() - 2));
|
||||
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
|
||||
@@ -143,6 +162,7 @@ public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
@ExceptionHandler({ Exception.class })
|
||||
public ResponseEntity<Object> handleAll(final Exception ex, final WebRequest request) {
|
||||
logger.info(ex.getClass().getName());
|
||||
logger.error("error", ex);
|
||||
//
|
||||
final ApiError apiError = new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex.getLocalizedMessage(), "error occurred");
|
||||
return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus());
|
||||
|
||||
Reference in New Issue
Block a user