SPR-6709 Handle RequestBodyNotValidException and update reference docs

This commit is contained in:
Rossen Stoyanchev
2011-06-02 18:27:50 +00:00
parent bfecedf5e2
commit 9d2ee7061c
4 changed files with 44 additions and 4 deletions

View File

@@ -17,6 +17,7 @@
package org.springframework.web.servlet.mvc.method.annotation.support;
import org.springframework.validation.Errors;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestBody;
/**
@@ -44,5 +45,15 @@ public class RequestBodyNotValidException extends RuntimeException {
public Errors getErrors() {
return errors;
}
@Override
public String getMessage() {
StringBuilder sb = new StringBuilder("Request body content validation failed: ");
sb.append(errors.getErrorCount()).append(" errors");
for (ObjectError error : errors.getAllErrors()) {
sb.append('\n').append(error);
}
return sb.toString();
}
}

View File

@@ -16,13 +16,14 @@
package org.springframework.web.servlet.mvc.support;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.core.Ordered;
@@ -37,6 +38,7 @@ import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
import org.springframework.web.servlet.mvc.method.annotation.support.RequestBodyNotValidException;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
/**
@@ -116,6 +118,9 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
else if (ex instanceof HttpMessageNotWritableException) {
return handleHttpMessageNotWritable((HttpMessageNotWritableException) ex, request, response, handler);
}
else if (ex instanceof RequestBodyNotValidException) {
return handleRequestBodyNotValidException((RequestBodyNotValidException) ex, request, response, handler);
}
}
catch (Exception handlerException) {
logger.warn("Handling of [" + ex.getClass().getName() + "] resulted in Exception", handlerException);
@@ -314,4 +319,20 @@ public class DefaultHandlerExceptionResolver extends AbstractHandlerExceptionRes
return new ModelAndView();
}
/**
* Handle the case where the object created from the body of a request has failed validation. The default
* implementation sends an HTTP 500 error along with a message containing the errors.
* @param request current HTTP request
* @param response current HTTP response
* @param handler the executed handler, or <code>null</code> if none chosen
* at the time of the exception (for example, if multipart resolution failed)
* @return a ModelAndView to render, or <code>null</code> if handled directly
* @throws Exception an Exception that should be thrown as result of the servlet request
*/
protected ModelAndView handleRequestBodyNotValidException(RequestBodyNotValidException ex,
HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, ex.getMessage());
return new ModelAndView();
}
}