Java 使用响应状态代码处理Spring MVC错误

Java 使用响应状态代码处理Spring MVC错误,java,spring,spring-mvc,Java,Spring,Spring Mvc,请在这个问题上帮助我。 我正在处理的项目是旧的mvc,不会更改为rest,因此必须处理我们所拥有的:。 这是我的控制器方法,它的类是anotated@controller 这是handleError方法体 private String handleError(Exception ex, String operation, SomeCustomListenerClass listener) { if (!listener.hasErrors()) { log.err

请在这个问题上帮助我。 我正在处理的项目是旧的mvc,不会更改为rest,因此必须处理我们所拥有的:。 这是我的控制器方法,它的类是anotated@controller

这是handleError方法体

    private String handleError(Exception ex, String operation, SomeCustomListenerClass listener) {
    if (!listener.hasErrors()) {
        log.error("Unexpected error getting notification detail", ex);
        listener.error("notification.controllerException", operation);
    }
    return "error";
}
现在我在客户端得到了正确的错误,比如在浏览器中,但也得到了状态代码500

现在我的老板说,当验证错误出现时,我们必须得到400,而不是现在的500。
所以,请帮助我,伙计们,如何克服这个问题。

尝试@ExceptionHandler注释或@ControllerAdvice创建自定义异常处理机制:

将@ResponseStatusHttpStatus.BAD_请求添加到handleError顶部。。。方法

@ExceptionHandler({ Throwable.class })
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String handleError(...) {
   ...
}

您可以扩展异常并将其抛出控制器:

@ResponseStatus(value=HttpStatus.BAD_REQUEST, reason="Your exception message")  
public class YourCustomException extends RuntimeException {
}
或者,您可以使用ExceptionControllerHandler:

@Controller
public class ExceptionHandlingController {

  // @RequestHandler methods
  ...
  
  // Exception handling methods
  
  // Convert a predefined exception to an HTTP Status code
  @ResponseStatus(value=HttpStatus.CONFLICT,
                  reason="Data integrity violation")  // 409
  @ExceptionHandler(DataIntegrityViolationException.class)
  public void conflict() {
    // Nothing to do
  }
  
  // Specify name of a specific view that will be used to display the error:
  @ExceptionHandler({SQLException.class,DataAccessException.class})
  public String databaseError() {
    // Nothing to do.  Returns the logical view name of an error page, passed
    // to the view-resolver(s) in usual way.
    // Note that the exception is NOT available to this view (it is not added
    // to the model) but see "Extending ExceptionHandlerExceptionResolver"
    // below.
    return "databaseError";
  }

  // Total control - setup a model and return the view name yourself. Or
  // consider subclassing ExceptionHandlerExceptionResolver (see below).
  @ExceptionHandler(Exception.class)
  public ModelAndView handleError(HttpServletRequest req, Exception ex) {
    logger.error("Request: " + req.getRequestURL() + " raised " + ex);

    ModelAndView mav = new ModelAndView();
    mav.addObject("exception", ex);
    mav.addObject("url", req.getRequestURL());
    mav.setViewName("error");
    return mav;
  }
}

我确实像你说的那样,但无论如何,它返回500,不知道为什么它不起作用。你的方法上有@ExceptionHandler吗。你能试一下更新的答案吗。您是否能够调试,调试器点是否进入此方法?它不是spring boot应用程序,它是mvc,而不是rest,如果我喜欢您所显示的,我将不会填充所需的错误字段,只会向客户端提供状态,但我还需要提供错误和状态。请尝试以下操作:
@Controller
public class ExceptionHandlingController {

  // @RequestHandler methods
  ...
  
  // Exception handling methods
  
  // Convert a predefined exception to an HTTP Status code
  @ResponseStatus(value=HttpStatus.CONFLICT,
                  reason="Data integrity violation")  // 409
  @ExceptionHandler(DataIntegrityViolationException.class)
  public void conflict() {
    // Nothing to do
  }
  
  // Specify name of a specific view that will be used to display the error:
  @ExceptionHandler({SQLException.class,DataAccessException.class})
  public String databaseError() {
    // Nothing to do.  Returns the logical view name of an error page, passed
    // to the view-resolver(s) in usual way.
    // Note that the exception is NOT available to this view (it is not added
    // to the model) but see "Extending ExceptionHandlerExceptionResolver"
    // below.
    return "databaseError";
  }

  // Total control - setup a model and return the view name yourself. Or
  // consider subclassing ExceptionHandlerExceptionResolver (see below).
  @ExceptionHandler(Exception.class)
  public ModelAndView handleError(HttpServletRequest req, Exception ex) {
    logger.error("Request: " + req.getRequestURL() + " raised " + ex);

    ModelAndView mav = new ModelAndView();
    mav.addObject("exception", ex);
    mav.addObject("url", req.getRequestURL());
    mav.setViewName("error");
    return mav;
  }
}