org.springframework.boot.web.error.ErrorAttributeOptions Java Examples

The following examples show how to use org.springframework.boot.web.error.ErrorAttributeOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CustomBasicErrorController.java    From nimrod with MIT License 6 votes vote down vote up
/**
     * html
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @return ModelAndView
     */
    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
        HttpStatus httpStatus = getStatus(request);
        int code = httpStatus.value();
//        Map<String, Object> errorAttributes = getErrorAttributes(request, true);
        Map<String, Object> errorAttributes = getErrorAttributes(request, ErrorAttributeOptions.defaults());
        LOGGER.info("status={},exception={}", code, errorAttributes);
        code = RestControllerAdviceHandler.codeSwitch(code);
        httpStatus = HttpStatus.valueOf(code);
//        Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(
//                request, true));
        Map<String, Object> model = Collections.unmodifiableMap(getErrorAttributes(request, ErrorAttributeOptions.defaults()));
        ModelAndView modelAndView = resolveErrorView(request, response, httpStatus, model);
        return (modelAndView == null ? new ModelAndView(code + "", model, httpStatus) : modelAndView);
    }
 
Example #2
Source File: ErrorHandlingConfig.java    From guardedbox with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Error handler.
 *
 * @return Unauthorized (401), Forbidden (403) or Not Found (404) with no body.
 */
@RequestMapping(value = "/error", method = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT,
        RequestMethod.DELETE, RequestMethod.HEAD, RequestMethod.OPTIONS, RequestMethod.PATCH, RequestMethod.TRACE})
public ResponseEntity<?> error() {

    Map<String, Object> requestErrorAttributes = errorAttributes.getErrorAttributes(
            new ServletWebRequest(request), ErrorAttributeOptions.defaults());
    HttpStatus errorStatus = HttpStatus.valueOf((Integer) requestErrorAttributes.get("status"));

    if (HttpStatus.UNAUTHORIZED.equals(errorStatus) || HttpStatus.FORBIDDEN.equals(errorStatus)) {
        session.invalidate();
        return new ResponseEntity<>(errorStatus);
    } else {
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }

}
 
Example #3
Source File: CustomBasicErrorController.java    From nimrod with MIT License 5 votes vote down vote up
/**
 * json
 *
 * @param request  HttpServletRequest
 * @param response HttpServletResponse
 * @return ResponseEntity<FailureEntity>
 */
@RequestMapping
@ResponseBody
public ResponseEntity<FailureEntity> error(HttpServletRequest request, HttpServletResponse response) {
    HttpStatus httpStatus = getStatus(request);
    int code = httpStatus.value();
    Map<String, Object> errorAttributes = getErrorAttributes(request, ErrorAttributeOptions.defaults());
    LOGGER.info("status={},exception={}", code, errorAttributes);
    code = RestControllerAdviceHandler.codeSwitch(code);
    httpStatus = HttpStatus.valueOf(code);
    return new ResponseEntity<>(new FailureEntity((String) errorAttributes.get("error"), httpStatus.value()), httpStatus);
}