【问题标题】:ExceptionHandler invokes but doesn't affect http responseExceptionHandler 调用但不影响 http 响应
【发布时间】:2017-05-02 16:38:06
【问题描述】:

我有以下方法:

    @ExceptionHandler(InvalidPriceUpdateException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public String handleInvalidPriceUpdateException(InvalidPriceUpdateException e) throws JsonProcessingException {
        return objectMapper.writeValueAsString(new HttpErrorDTO(e.getMessage()));
    }

我看到(在调试中)它调用但在浏览器中我看到 500 错误(而不是 HttpStatus.BAD_REQUEST)。并且http响应的内容包含异常信息(而不是HttpErrorDTO结构)。

我的代码有什么问题?

【问题讨论】:

    标签: java spring spring-mvc exception-handling exceptionhandler


    【解决方案1】:

    这是因为您正在返回一个 String,您应该在其中真正构建整个 ResponseEntity。例如,阅读here 的含义以及如何构建一个。

    【讨论】:

      猜你喜欢
      • 2018-12-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多