SPR-8429 Return 400 instead of 500 for required header/cookie/pathvar. This is also more in line with jax-rs.

This commit is contained in:
Rossen Stoyanchev
2011-06-21 15:03:12 +00:00
parent 41e5d55d52
commit 5f76ad809f
5 changed files with 84 additions and 64 deletions

View File

@@ -885,14 +885,8 @@ public class ServletAnnotationControllerTests {
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "other");
MockHttpServletResponse response = new MockHttpServletResponse();
try {
servlet.service(request, response);
fail("Should have failed because of type-level parameter constraint not met");
}
catch (ServletException ex) {
// expected
ex.printStackTrace();
}
servlet.service(request, response);
assertEquals(400, response.getStatus());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("active", "true");
@@ -905,14 +899,8 @@ public class ServletAnnotationControllerTests {
request.addParameter("view", "my");
request.addParameter("lang", "de");
response = new MockHttpServletResponse();
try {
servlet.service(request, response);
fail("Should have failed because of type-level parameter constraint not met");
}
catch (ServletException ex) {
// expected
ex.printStackTrace();
}
servlet.service(request, response);
assertEquals(400, response.getStatus());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "my");

View File

@@ -30,6 +30,7 @@ import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.support.ModelAndViewContainer;
@@ -85,7 +86,7 @@ public class PathVariableMethodArgumentResolverTests {
assertEquals("PathVariable not added to the model", "value", mavContainer.getAttribute("name"));
}
@Test(expected = IllegalStateException.class)
@Test(expected = ServletRequestBindingException.class)
public void handleMissingValue() throws Exception {
resolver.resolveArgument(paramNamedString, mavContainer, webRequest, null);
fail("Unresolved path variable should lead to exception.");

View File

@@ -35,6 +35,7 @@ import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.support.RequestBodyNotValidException;
import org.springframework.web.servlet.mvc.multiaction.NoSuchRequestHandlingMethodException;
@@ -96,6 +97,16 @@ public class DefaultHandlerExceptionResolverTests {
assertEquals("Invalid status code", 400, response.getStatus());
}
@Test
public void handleServletRequestBindingException() {
String message = "Missing required value - header, cookie, or pathvar";
ServletRequestBindingException ex = new ServletRequestBindingException(message);
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
assertNotNull("No ModelAndView returned", mav);
assertTrue("No Empty ModelAndView returned", mav.isEmpty());
assertEquals("Invalid status code", 400, response.getStatus());
}
@Test
public void handleTypeMismatch() {
TypeMismatchException ex = new TypeMismatchException("foo", String.class);