Reputation: 224
I want to handle in my app engine application the error 400.
I could handle the 404 error using the following code:
@RequestMapping("/**")
public void unmappedRequest(HttpServletRequest request) {
request.getRequestURI();
String uri = request.getRequestURI();
throw new UnknownResourceException("There is no resource for path "
+ uri);
}
and then i manage the 404 error.
However, for the 400 error (bad request), I tried something like this:
in web.xml
<error-page>
<error-code>400</error-code>
<location>/error/400</location>
</error-page>
and then in my controller
@RequestMapping("/error/400")
public void badRequest(HttpServletRequest request) {
request.getRequestURI();
String uri = request.getRequestURI();
throw new UnknownResourceException("bad request for path " + uri);
}
But it doesn't work, so I'm getting the default error screen from app engine when I make a bad request.
Any suggestions?
Upvotes: 0
Views: 2080
Reputation: 224
the easiest and quickest solution I got finally was doing something like this:
@ControllerAdvice
public class ControllerHandler {
@ExceptionHandler(MissingServletRequestParameterException.class)
public String handleMyException(Exception exception,
HttpServletRequest request) {
return "/error/myerror";
}
}
The key here is to handle the org.springframework.web.bind.MissingServletRequestParameterException;
Other alternative, It also can be done through the web.xml, like the following:
<error-page>
<exception-type>org.springframework.web.bind.MissingServletRequestParameterException</exception-type>
<location>/WEB-INF/error/myerror.jsp</location>
</error-page>
Upvotes: 3
Reputation: 279990
The entry
<error-page>
<error-code>400</error-code>
<location>/error/400</location>
</error-page>
results in the servlet container making a RequestDispatcher#forward
to the location
element. This won't map to a @Controller
, but to a servlet (url-mapped) or a jsp, or other.
Use an @ExceptionHandler
. For examples (with specific Exceptions), see here.
Upvotes: 2