Created
March 2, 2012 14:50
-
-
Save danieldbower/1958870 to your computer and use it in GitHub Desktop.
Serializing BindingResult Errors to JSON
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
package model; | |
import java.util.List; | |
import java.util.Map; | |
import org.codehaus.jackson.annotate.JsonAutoDetect; | |
import org.codehaus.jackson.annotate.JsonAutoDetect.Visibility; | |
import org.codehaus.jackson.annotate.JsonProperty; | |
import org.springframework.validation.BindingResult; | |
import org.springframework.validation.FieldError; | |
import com.google.common.collect.Lists; | |
import com.google.common.collect.Maps; | |
@JsonAutoDetect(fieldVisibility=Visibility.NONE, getterVisibility=Visibility.NONE, isGetterVisibility=Visibility.NONE) | |
public class ValidationException extends Exception{ | |
private static final long serialVersionUID = 1L; | |
@JsonProperty | |
private boolean success = false; | |
@JsonProperty | |
private List<String> errors = Lists.newArrayList(); | |
@JsonProperty | |
private Map<String,String> fieldErrors = Maps.newHashMap(); | |
public ValidationException(){} | |
public ValidationException(BindingResult bindingResult){ | |
for(FieldError error : bindingResult.getFieldErrors()){ | |
fieldErrors.put(error.getField(), error.getDefaultMessage()); | |
} | |
} | |
public boolean getSuccess(){ | |
return success; | |
} | |
public List<String> getErrors(){ | |
return errors; | |
} | |
public Map<String, String> getFieldErrors(){ | |
return fieldErrors; | |
} | |
} | |
/* | |
* The following two classes are from https://github.com/rstoyanchev/spring-mvc-31-demo/tree/master/src/main/java/org/springframework/samples/mvc31/exceptionhandler | |
*/ | |
package web.exceptions; | |
import org.springframework.web.bind.annotation.ExceptionHandler; | |
import org.springframework.web.bind.annotation.ResponseBody; | |
public class GlobalExceptionHandler { | |
/** | |
* This method basically just serializes the exception to JSON | |
*/ | |
@ExceptionHandler(value=ValidationException.class) | |
public @ResponseBody ValidationException handle(ValidationException exception) { | |
return exception; | |
} | |
} | |
package web.exceptions; | |
import java.lang.reflect.Method; | |
import org.springframework.web.bind.annotation.ExceptionHandler; | |
import org.springframework.web.method.HandlerMethod; | |
import org.springframework.web.method.annotation.ExceptionHandlerMethodResolver; | |
import org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver; | |
import org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod; | |
/** | |
* Extends {@link ExceptionHandlerExceptionResolver} to provide "global" | |
* {@code @ExceptionHandler} methods for use across all controllers. | |
* | |
* <p> | |
* Plugged in via | |
* {@link WebConfig#configureHandlerExceptionResolvers(java.util.List)}. | |
*/ | |
public class ExtendedExceptionHandlerExceptionResolver extends | |
ExceptionHandlerExceptionResolver { | |
private Object handler; | |
private ExceptionHandlerMethodResolver methodResolver; | |
/** | |
* Provide a handler with @{@link ExceptionHandler} methods. | |
*/ | |
public void setExceptionHandler(Object handler) { | |
this.handler = handler; | |
this.methodResolver = new ExceptionHandlerMethodResolver( | |
handler.getClass()); | |
} | |
@Override | |
protected ServletInvocableHandlerMethod getExceptionHandlerMethod( | |
HandlerMethod handlerMethod, Exception exception) { | |
ServletInvocableHandlerMethod result = super.getExceptionHandlerMethod( | |
handlerMethod, exception); | |
if (result != null) { | |
return result; | |
} | |
Method method = this.methodResolver.resolveMethod(exception); | |
return (method != null) ? new ServletInvocableHandlerMethod( | |
this.handler, method) : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment