/** * Invoked when a named value is required, but {@link #resolveName(String, MethodParameter, NativeWebRequest)} * returned {@code null} and there is no default value. Subclasses typically throw an exception in this case. * @param name the name for the value * @param parameter the method parameter */ protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing argument '" + name + "' for method parameter of type " + parameter.getNestedParameterType().getSimpleName()); }
@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing request attribute '" + name + "' of type " + parameter.getNestedParameterType().getSimpleName()); }
@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing session attribute '" + name + "' of type " + parameter.getNestedParameterType().getSimpleName()); }
@RequestMapping("/") public void handleRequest() throws Exception { throw new ServletRequestBindingException("message"); } }
@RequestMapping("/") public void handleRequest() throws Exception { throw new IllegalStateException(new ServletRequestBindingException("message")); } }
/** * Invoked when a named value is required, but {@link #resolveName(String, MethodParameter, NativeWebRequest)} * returned {@code null} and there is no default value. Subclasses typically throw an exception in this case. * @param name the name for the value * @param parameter the method parameter */ protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing argument '" + name + "' for method parameter of type " + parameter.getNestedParameterType().getSimpleName()); }
@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing session attribute '" + name + "' of type " + parameter.getNestedParameterType().getSimpleName()); }
@Override protected void handleMissingValue(String name, MethodParameter parameter) throws ServletException { throw new ServletRequestBindingException("Missing request attribute '" + name + "' of type " + parameter.getNestedParameterType().getSimpleName()); }
protected final T parse(String name, String parameter) throws ServletRequestBindingException { validateRequiredParameter(name, parameter); try { return doParse(parameter); } catch (NumberFormatException ex) { throw new ServletRequestBindingException( "Required " + getType() + " parameter '" + name + "' with value of '" + parameter + "' is not a valid number", ex); } }
if (found) { String paramType = parameter.getNestedParameterType().getName(); throw new ServletRequestBindingException( "Found more than one match for URI path parameter '" + name + "' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.");
/** * Treats errors as fatal. * <p>Use this method only if it's an error if the input isn't valid. * This might be appropriate if all input is from dropdowns, for example. * @throws ServletRequestBindingException subclass of ServletException on any binding problem */ public void closeNoCatch() throws ServletRequestBindingException { if (getBindingResult().hasErrors()) { throw new ServletRequestBindingException( "Errors binding onto object '" + getBindingResult().getObjectName() + "'", new BindException(getBindingResult())); } }
protected final T parse(String name, String parameter) throws ServletRequestBindingException { validateRequiredParameter(name, parameter); try { return doParse(parameter); } catch (NumberFormatException ex) { throw new ServletRequestBindingException( "Required " + getType() + " parameter '" + name + "' with value of '" + parameter + "' is not a valid number", ex); } }
@Test public void servletRequestBindingException() { Exception ex = new ServletRequestBindingException("message"); testException(ex); }
if (found) { String paramType = parameter.getNestedParameterType().getName(); throw new ServletRequestBindingException( "Found more than one match for URI path parameter '" + name + "' for parameter type [" + paramType + "]. Use 'pathVar' attribute to disambiguate.");
/** * Treats errors as fatal. * <p>Use this method only if it's an error if the input isn't valid. * This might be appropriate if all input is from dropdowns, for example. * @throws ServletRequestBindingException subclass of ServletException on any binding problem */ public void closeNoCatch() throws ServletRequestBindingException { if (getBindingResult().hasErrors()) { throw new ServletRequestBindingException( "Errors binding onto object '" + getBindingResult().getObjectName() + "'", new BindException(getBindingResult())); } }
@Test public void controllerAdviceWithNestedException() { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class); ctx.refresh(); ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); resolver.setApplicationContext(ctx); resolver.afterPropertiesSet(); IllegalStateException ex = new IllegalStateException(new ServletRequestBindingException("message")); assertNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex)); }
throw new ServletRequestBindingException("servlet");
@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 controllerAdvice() throws Exception { StaticWebApplicationContext ctx = new StaticWebApplicationContext(); ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class); ctx.refresh(); ExceptionHandlerExceptionResolver resolver = new ExceptionHandlerExceptionResolver(); resolver.setApplicationContext(ctx); resolver.afterPropertiesSet(); ServletRequestBindingException ex = new ServletRequestBindingException("message"); assertNotNull(resolver.resolveException(this.servletRequest, this.servletResponse, null, ex)); assertEquals(400, this.servletResponse.getStatus()); assertEquals("error content", this.servletResponse.getContentAsString()); assertEquals("someHeaderValue", this.servletResponse.getHeader("someHeader")); }
/** * Treats errors as fatal. * <p> * Use this method only if it's an error if the input isn't valid. This * might be appropriate if all input is from dropdowns, for example. * * @throws ServletRequestBindingException subclass of ServletException * on any binding problem */ public void closeNoCatch() throws ServletRequestBindingException { if (getBindingResult().hasErrors()) { throw new ServletRequestBindingException("Errors binding onto object '" + getBindingResult().getObjectName() + "'", new BindException( getBindingResult())); } }