2014-02-13 62 views
1

我用從this頁#3方法時,在生產模式,但是我的錯誤頁面返回200 OK HTTP狀態代碼,自定義應用程序異常頁,而我希望它返回500內部服務器錯誤(因爲它在開發環境中)。自定義應用程序異常HTTP狀態代碼

我不知道如何改變狀態代碼。有任何想法嗎?

回答

1

在handleRequestException實現將使用默認的DefaultRequestExceptionHandler的,你可以看到,響應狀態設置爲500:

response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 

你必須做你的代碼非常類似的東西(未測試):

public RequestExceptionHandler decorateRequestExceptionHandler(final Logger logger, final ResponseRenderer renderer, 
    final ComponentSource componentSource, final Response response, 
    @Symbol(SymbolConstants.PRODUCTION_MODE) boolean productionMode, Object service) { 
    if (!productionMode) return null; 

    return new RequestExceptionHandler() { 
     public void handleRequestException(Throwable exception) throws IOException { 
      logger.error("Unexpected runtime exception: " + exception.getMessage(), exception); 
      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); 
      ExceptionReporter index = (ExceptionReporter) componentSource.getPage("Index"); 

      index.reportException(exception); 

      renderer.renderPageMarkupResponse("Index"); 
     } 
    }; 
}