minor fix

This commit is contained in:
DOHA 2016-01-19 21:25:24 +02:00 committed by David Morley
parent a0ee0ccabc
commit 4800e6d511
4 changed files with 52 additions and 13 deletions

View File

@ -92,6 +92,13 @@
<version>${jstl.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${javax.validation.version}</version>
</dependency>
<!-- marshalling -->
@ -283,6 +290,7 @@
<!-- various -->
<hibernate-validator.version>5.2.2.Final</hibernate-validator.version>
<javax.servlet-api.version>3.0.1</javax.servlet-api.version>
<javax.validation.version>1.1.0.Final</javax.validation.version>
<jstl.version>1.2</jstl.version>
<jackson.version>2.2.2</jackson.version>
<springfox.version>2.2.2</springfox.version>

View File

@ -2,10 +2,14 @@ package org.baeldung.persistence.model;
import java.io.Serializable;
import javax.validation.constraints.Size;
public class Foo implements Serializable {
private long id;
@Size(min = 5, max = 14)
private String name;
public Foo() {

View File

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

View File

@ -32,9 +32,10 @@ public class FooLiveTest {
assertEquals(HttpStatus.BAD_REQUEST, error.getStatus());
assertEquals(1, error.getErrors().size());
assertTrue(error.getErrors().get(0).contains("should be of type"));
}
@Test
public void whenNoHandlerForHttpRequest_thenNotFound() {
final Response response = givenAuth().delete(URL_PREFIX + "/api/xx");
@ -42,6 +43,8 @@ public class FooLiveTest {
assertEquals(HttpStatus.NOT_FOUND, error.getStatus());
assertEquals(1, error.getErrors().size());
assertTrue(error.getErrors().get(0).contains("No handler found"));
System.out.println(response.asString());
}
@Test
@ -51,6 +54,8 @@ public class FooLiveTest {
assertEquals(HttpStatus.METHOD_NOT_ALLOWED, error.getStatus());
assertEquals(1, error.getErrors().size());
assertTrue(error.getErrors().get(0).contains("Supported methods are"));
System.out.println(response.asString());
}
@Test
@ -60,6 +65,8 @@ public class FooLiveTest {
assertEquals(HttpStatus.UNSUPPORTED_MEDIA_TYPE, error.getStatus());
assertEquals(1, error.getErrors().size());
assertTrue(error.getErrors().get(0).contains("media type is not supported"));
System.out.println(response.asString());
}
}