3

我想处理正常和休息/ajax 请求的异常。这是我的代码,

@ControllerAdvice
public class MyExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ModelAndView handleCustomException(Exception ex) {

        ModelAndView model = new ModelAndView("error");
        model.addObject("errMsg", ex.getMessage());
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        ex.printStackTrace(pw);
        sw.toString();
        model.addObject("errTrace", sw);
        return model;

    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String handleAjaxException(Exception ex) {
        JSONObject model = new JSONObject();
        model.put("status", "error");
        model.put("errMsg", ex.getMessage());
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        ex.printStackTrace(pw);
        sw.toString();
        model.put("errTrace", sw);

        return model.toString();
    }
}

这会给我一个错误,因为我不能有两次 @ExceptionHandler(Exception.class) 。那么解决方案可能是什么?

4

4 回答 4

5

查看@ControllerAdvice的配置:http: //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice.html

所以你可以创建两个类(错误处理程序)并指定 annotations/basePackages/assignibaleTypes

例如,对于 REST(ajax),对控制器使用 @RestController 注释,您可以处理如下错误:

@ControllerAdvice(annotations = RestController.class)
public class MyExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public String handleAjaxException(Exception ex) {
        ...
    }
}

对于其他情况,它可以是带有注释的错误处理程序

@ControllerAdvice(annotations = Controller.class)
于 2015-08-18T11:39:59.313 回答
2

这是spring mvc中的全局异常处理程序。每次在您的应用程序中发现异常时都会调用它。我认为您只能在web.xml的帮助下控制404异常。

@ControllerAdvice
public class GlobalExceptionController {

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public ModelAndView handleAllException(Throwable ex,
            HttpServletResponse response) {

        ex.printStackTrace();
        // Set Status
        response.setStatus(500);
        // Set View
        ModelAndView model = new ModelAndView("500");
        model.addObject("navlabel", "");
        model.addObject("userActivity", new ArrayList<String>());
        // Set exception Message
        model.addObject("errMsg", ex.getMessage());
        return model;
    }
}
于 2015-08-18T11:33:08.713 回答
0

您可以创建内部静态类@RestControllerAdvice。不需要为此创建单独的@RestController。

@ControllerAdvice
public class BaseController {
private static final Logger logger = 
LoggerFactory.getLogger(BaseController.class);

@ExceptionHandler(Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public String handleException(Exception error, Model model) {
    logger.error("Error was: " + error.getMessage(), error);
    model.addAttribute("message", error.getMessage());
    model.addAttribute("stackTrace", error.getStackTrace());
    model.addAttribute("exception", error);
    return "error"; //return view
}

@RestControllerAdvice
public static class RestBaseController {

    private static final Logger logger = LoggerFactory.getLogger(RestBaseController.class);

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleException(Exception error) {
        logger.error("Error was: " + error.getMessage(), error);

        return "error"; //return "error"
    }
}
}
于 2017-04-27T13:41:04.807 回答
0

您可以编写两个异常处理程序来处理正常和 rest/ajax 请求异常。这是说明解决方案的示例代码。

@ControllerAdvice(annotations = RestController.class)
@Order(1)
class RestExceptionHandler {
    @ExceptionHandler(MyException.class)
    @ResponseBody
    ResponseEntity<ErrorResponse> exceptionHandler() {
        ....
    }   
}

@ControllerAdvice(annotations = Controller.class)
@Order(2)
class ExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ModelAndView handleError500(HttpServletRequest request, HttpServletResponse response, Exception ex) {
        ModelAndView mav = new ModelAndView("error");
        mav.addObject("error", "500");
        return mav;
    }
}
于 2017-10-09T05:18:35.680 回答