From b9b9cfba50d8902cb6dd84c30b75d6f774ce1b76 Mon Sep 17 00:00:00 2001 From: Lucas Espindola Date: Tue, 21 Jan 2014 02:01:01 -0200 Subject: [PATCH 1/2] return ErrorReponse when request contains invalid JSON or HTTP method --- .../util/GlobalExceptionHandler.java | 26 +++++++++++++++++++ .../viktor/javawebpoc/l10n/MessageKey.java | 2 ++ .../l10n/javawebpocException.properties | 16 +++++++----- .../l10n/javawebpocException_pt_BR.properties | 18 ++++++++----- .../l10n/javawebpocValidation.properties | 16 ++++++------ .../javawebpocValidation_pt_BR.properties | 15 ++++++++--- 6 files changed, 69 insertions(+), 24 deletions(-) diff --git a/javawebpoc-web/src/main/java/br/com/viktor/javawebpoc/controller/util/GlobalExceptionHandler.java b/javawebpoc-web/src/main/java/br/com/viktor/javawebpoc/controller/util/GlobalExceptionHandler.java index 4aa4f00..ac9e143 100644 --- a/javawebpoc-web/src/main/java/br/com/viktor/javawebpoc/controller/util/GlobalExceptionHandler.java +++ b/javawebpoc-web/src/main/java/br/com/viktor/javawebpoc/controller/util/GlobalExceptionHandler.java @@ -5,6 +5,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.http.HttpStatus; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; @@ -14,6 +16,9 @@ import br.com.viktor.javawebpoc.exception.alreadyExists.AlreadyExistsException; import br.com.viktor.javawebpoc.exception.invalidArgument.InvalidArgumentException; import br.com.viktor.javawebpoc.exception.notFound.NotFoundException; +import br.com.viktor.javawebpoc.l10n.MessageKey; + +import com.fasterxml.jackson.core.JsonParseException; @ControllerAdvice public class GlobalExceptionHandler { @@ -53,4 +58,25 @@ public ErrorResponse generalExceptionHandler(JavaWebPoCException ex, Locale loca return response; } + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + @ResponseStatus(value = HttpStatus.NOT_IMPLEMENTED) + @ResponseBody + public ErrorResponse jsonErrorHandler(HttpRequestMethodNotSupportedException ex, Locale locale){ + ErrorResponse response = new ErrorResponse(); + response.setMessage(messageSource.getMessage(MessageKey.METHODNOTSUPPORTED_EXCEPTION.getMessageKey(), new Object[]{}, locale)); + return response; + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + @ResponseStatus(value = HttpStatus.BAD_REQUEST) + @ResponseBody + public ErrorResponse httpMessageInvalidErrorHandler(HttpMessageNotReadableException ex, Locale locale){ + if(ex.getCause() instanceof JsonParseException){ + ErrorResponse response = new ErrorResponse(); + response.setMessage(messageSource.getMessage(MessageKey.JSONBADREQUEST_EXCEPTION.getMessageKey(), new Object[]{}, locale)); + return response; + } + return null; + } + } \ No newline at end of file diff --git a/javawebpoc-web/src/main/java/br/com/viktor/javawebpoc/l10n/MessageKey.java b/javawebpoc-web/src/main/java/br/com/viktor/javawebpoc/l10n/MessageKey.java index 9a93ef1..4c3814c 100644 --- a/javawebpoc-web/src/main/java/br/com/viktor/javawebpoc/l10n/MessageKey.java +++ b/javawebpoc-web/src/main/java/br/com/viktor/javawebpoc/l10n/MessageKey.java @@ -8,6 +8,8 @@ public enum MessageKey { NULLARGUMENT_EXCEPTION("br.com.viktor.javawebpoc.exception.nullargumentexception"), INVALIDSTUDENT_EXCEPTION("br.com.viktor.javawebpoc.exception.invalidstudentexception"), NOTFOUND_EXCEPTION("br.com.viktor.javawebpoc.exception.notfoundexception"), + JSONBADREQUEST_EXCEPTION("br.com.viktor.javawebpoc.exception.jsonbadrequest"), + METHODNOTSUPPORTED_EXCEPTION("br.com.viktor.javawebpoc.exception.methodnotsupported"), VALIDATION_INVALID("br.com.viktor.javawebpoc.validation.invalid"), VALIDATION_FIELD_REQUIRED("br.com.viktor.javawebpoc.validation.fieldrequired"), diff --git a/javawebpoc-web/src/main/resources/l10n/javawebpocException.properties b/javawebpoc-web/src/main/resources/l10n/javawebpocException.properties index 494a397..72b139e 100644 --- a/javawebpoc-web/src/main/resources/l10n/javawebpocException.properties +++ b/javawebpoc-web/src/main/resources/l10n/javawebpocException.properties @@ -1,10 +1,14 @@ -br.com.viktor.javawebpoc.exception.javawebpocexception=There was an internal error +br.com.viktor.javawebpoc.exception.javawebpocexception=There was an internal error. -br.com.viktor.javawebpoc.exception.alreadyexistsexception=Argument already exists +br.com.viktor.javawebpoc.exception.alreadyexistsexception=Argument already exists. br.com.viktor.javawebpoc.exception.studentexistsexception=Student already exists: {0} -br.com.viktor.javawebpoc.exception.nullargumentexception=Null argument: {0} -br.com.viktor.javawebpoc.exception.argumentexception=Invalid argument: {0} -br.com.viktor.javawebpoc.exception.invalidstudentexception=Invalid Student: {0} +br.com.viktor.javawebpoc.exception.nullargumentexception=Null argument: {0}. +br.com.viktor.javawebpoc.exception.argumentexception=Invalid argument: {0}. +br.com.viktor.javawebpoc.exception.invalidstudentexception=Invalid Student: {0}. + +br.com.viktor.javawebpoc.exception.notfoundexception=Not found. + +br.com.viktor.javawebpoc.exception.jsonbadrequest=JSON has an invalid format. +br.com.viktor.javawebpoc.exception.methodnotsupported=HTTP method not supported. -br.com.viktor.javawebpoc.exception.notfoundexception=Not found \ No newline at end of file diff --git a/javawebpoc-web/src/main/resources/l10n/javawebpocException_pt_BR.properties b/javawebpoc-web/src/main/resources/l10n/javawebpocException_pt_BR.properties index 2507fc3..02ea4ad 100644 --- a/javawebpoc-web/src/main/resources/l10n/javawebpocException_pt_BR.properties +++ b/javawebpoc-web/src/main/resources/l10n/javawebpocException_pt_BR.properties @@ -1,10 +1,14 @@ -br.com.viktor.javawebpoc.exception.javawebpocexception=Houve um erro interno na aplicação +br.com.viktor.javawebpoc.exception.javawebpocexception=Houve um erro interno na aplicação. -br.com.viktor.javawebpoc.exception.alreadyexistsexception=Argumento já existente -br.com.viktor.javawebpoc.exception.studentexistsexception=Estudante já existente: {0} +br.com.viktor.javawebpoc.exception.alreadyexistsexception=Argumento já existente. +br.com.viktor.javawebpoc.exception.studentexistsexception=Estudante já existente: {0} -br.com.viktor.javawebpoc.exception.nullargumentexception=Argumento nulo: {0} -br.com.viktor.javawebpoc.exception.argumentexception=Argumento inválido: {0} -br.com.viktor.javawebpoc.exception.invalidstudentexception=Estudante inválido: {0} +br.com.viktor.javawebpoc.exception.nullargumentexception=Argumento nulo: {0}. +br.com.viktor.javawebpoc.exception.argumentexception=Argumento inválido: {0}. +br.com.viktor.javawebpoc.exception.invalidstudentexception=Estudante inválido: {0}. + +br.com.viktor.javawebpoc.exception.notfoundexception=Não encontrado. + +br.com.viktor.javawebpoc.exception.jsonbadrequest=O JSON está mal formado. +br.com.viktor.javawebpoc.exception.methodnotsupported=Método HTTP não disponível. -br.com.viktor.javawebpoc.exception.notfoundexception=Não encontrado \ No newline at end of file diff --git a/javawebpoc-web/src/main/resources/l10n/javawebpocValidation.properties b/javawebpoc-web/src/main/resources/l10n/javawebpocValidation.properties index 89afa2b..b2d964c 100644 --- a/javawebpoc-web/src/main/resources/l10n/javawebpocValidation.properties +++ b/javawebpoc-web/src/main/resources/l10n/javawebpocValidation.properties @@ -1,12 +1,12 @@ -br.com.viktor.javawebpoc.validation.studentinvalid=Invalid student -br.com.viktor.javawebpoc.validation.studentnamesize=Student's name have to be between {0} and {1} caracters long -br.com.viktor.javawebpoc.validation.studentnamenull=Student's name cannot be null +br.com.viktor.javawebpoc.validation.studentinvalid=Invalid student. +br.com.viktor.javawebpoc.validation.studentnamesize=Student's name have to be between {0} and {1} caracters long. +br.com.viktor.javawebpoc.validation.studentnamenull=Student's name cannot be null. -br.com.viktor.javawebpoc.student=estudante -br.com.viktor.javawebpoc.name=nome +br.com.viktor.javawebpoc.student=student +br.com.viktor.javawebpoc.name=name -br.com.viktor.javawebpoc.validation.invalid=Invalido {0}. -br.com.viktor.javawebpoc.validation.fieldrequired=A(o) {0} é obrigatório. -br.com.viktor.javawebpoc.validation.stringlength=A(o) {0} precisa ter entre {1} e {2} caracteres. +br.com.viktor.javawebpoc.validation.invalid=Invalid {0}. +br.com.viktor.javawebpoc.validation.fieldrequired=The {0} is required. +br.com.viktor.javawebpoc.validation.stringlength=The {0} needs to be between {1} and {2} characters. \ No newline at end of file diff --git a/javawebpoc-web/src/main/resources/l10n/javawebpocValidation_pt_BR.properties b/javawebpoc-web/src/main/resources/l10n/javawebpocValidation_pt_BR.properties index d7cac14..56bf032 100644 --- a/javawebpoc-web/src/main/resources/l10n/javawebpocValidation_pt_BR.properties +++ b/javawebpoc-web/src/main/resources/l10n/javawebpocValidation_pt_BR.properties @@ -1,3 +1,12 @@ -br.com.viktor.javawebpoc.validation.studentinvalid=Estudante inválido -br.com.viktor.javawebpoc.validation.studentnamesize=O nome do estudante precisa estar entre {0} e {1} caracters -br.com.viktor.javawebpoc.validation.studentnamenull=O nome do estudante não pode ser vazio \ No newline at end of file +br.com.viktor.javawebpoc.validation.studentinvalid=Estudante inválido. +br.com.viktor.javawebpoc.validation.studentnamesize=O nome do estudante precisa estar entre {0} e {1} caracteres. +br.com.viktor.javawebpoc.validation.studentnamenull=O nome do estudante não pode ser vazio. + + +br.com.viktor.javawebpoc.student=estudante +br.com.viktor.javawebpoc.name=nome + + +br.com.viktor.javawebpoc.validation.invalid=Invalido {0}. +br.com.viktor.javawebpoc.validation.fieldrequired=A(o) {0} é obrigatório. +br.com.viktor.javawebpoc.validation.stringlength=A(o) {0} precisa ter entre {1} e {2} caracteres. \ No newline at end of file From 50f48459f0c48fd70fc30ffb08bbb3910af86ded Mon Sep 17 00:00:00 2001 From: Lucas Espindola Date: Sat, 25 Jan 2014 14:36:40 -0200 Subject: [PATCH 2/2] fix messageSource encoding issue rename default message.properties add en_US --- ...xception.properties => javawebpocException_en_US.properties} | 0 ...idation.properties => javawebpocValidation_en_US.properties} | 0 javawebpoc-web/src/main/resources/localizationContext.xml | 2 +- 3 files changed, 1 insertion(+), 1 deletion(-) rename javawebpoc-web/src/main/resources/l10n/{javawebpocException.properties => javawebpocException_en_US.properties} (100%) rename javawebpoc-web/src/main/resources/l10n/{javawebpocValidation.properties => javawebpocValidation_en_US.properties} (100%) diff --git a/javawebpoc-web/src/main/resources/l10n/javawebpocException.properties b/javawebpoc-web/src/main/resources/l10n/javawebpocException_en_US.properties similarity index 100% rename from javawebpoc-web/src/main/resources/l10n/javawebpocException.properties rename to javawebpoc-web/src/main/resources/l10n/javawebpocException_en_US.properties diff --git a/javawebpoc-web/src/main/resources/l10n/javawebpocValidation.properties b/javawebpoc-web/src/main/resources/l10n/javawebpocValidation_en_US.properties similarity index 100% rename from javawebpoc-web/src/main/resources/l10n/javawebpocValidation.properties rename to javawebpoc-web/src/main/resources/l10n/javawebpocValidation_en_US.properties diff --git a/javawebpoc-web/src/main/resources/localizationContext.xml b/javawebpoc-web/src/main/resources/localizationContext.xml index f60453d..e93a90d 100644 --- a/javawebpoc-web/src/main/resources/localizationContext.xml +++ b/javawebpoc-web/src/main/resources/localizationContext.xml @@ -13,6 +13,6 @@ l10n/javawebpocValidation + - \ No newline at end of file