使用Spring 3在应用程序引擎中处理(400)错误 [英] Error handling (400) in app engine with Spring 3
问题描述
我可以使用以下代码处理404错误:
@RequestMapping(/ **)
public void unmappedRequest(HttpServletRequest request){
request.getRequestURI();
String uri = request.getRequestURI();
throw new UnknownResourceException(没有路径的资源
+ uri);
}
然后我管理404错误。
然而,对于400错误(不好的请求),我尝试过这样的一个例子:
在web.xml中
< error-page>
< error-code> 400< / error-code>
< location> / error / 400< / location>
< / error-page>
然后在我的控制器中
@RequestMapping(/ error / 400)
public void badRequest(HttpServletRequest request){
request.getRequestURI();
String uri = request.getRequestURI();
throw new UnknownResourceException(bad request for path+ uri);
}
但它不起作用,所以我得到默认错误屏幕从应用引擎发出一个不好的请求。
任何建议?
最简单快捷的解决方案我终于做了这样的事情:
@ControllerAdvice
public class ControllerHandler {
@ExceptionHandler(MissingServletRequestParameterException.class)
public String handleMyException(异常异常,
HttpServletRequest请求){
return/ error / myerror;
}
}
这里的关键是处理org.springframework。 web.bind。 MissingServletRequestParameterException ;
其他替代方法也可以通过web.xml完成,如下所示:
< error-page>
< exception-type> org.springframework.web.bind.MissingServletRequestParameterException< / exception-type>
< location> /WEB-INF/error/myerror.jsp< / location>
< / error-page>
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?
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>
这篇关于使用Spring 3在应用程序引擎中处理(400)错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!