2013-08-31 33 views
0

我想在我的App Engine应用程序来处理错误400

我可以使用下面的代码处理404错误:错误的应用程序引擎处理(400)与弹簧3

@RequestMapping("/**") 
public void unmappedRequest(HttpServletRequest request) { 
    request.getRequestURI(); 
    String uri = request.getRequestURI(); 
    throw new UnknownResourceException("There is no resource for path " 
    + 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); 
} 

但不起作用,所以当我提出错误的请求时,我会从应用程序引擎获取默认错误屏幕。
有什么建议吗?

回答

3

我得到了最简单,最快捷的解决方案总算是做这样的事情:

@ControllerAdvice 
public class ControllerHandler { 

    @ExceptionHandler(MissingServletRequestParameterException.class) 
    public String handleMyException(Exception exception, 
     HttpServletRequest request) { 
    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> 
2

条目

<error-page> 
    <error-code>400</error-code> 
    <location>/error/400</location> 
</error-page> 

导致servlet容器制造RequestDispatcher#forwardlocation元件。这不会映射到@Controller,而是映射到servlet(url映射)或jsp或其他。使用@ExceptionHandler。有关示例(特定例外),请参见here

相关问题