【问题标题】:Unable to use custom HttpMessageNotReadableException error message in Spring Boot无法在 Spring Boot 中使用自定义 HttpMessageNotReadableException 错误消息
【发布时间】:2019-08-02 09:16:28
【问题描述】:

我目前正在尝试为异常提供自定义消息,但遇到了 HttpMessageNotReadableException 的问题。

我有一个 ErrorDetails 类:

public class ErrorDetails {
    private Date timestamp;
    private String message;
    private String details;



    public ErrorDetails(Date timestamp, String message, String details) {
        super();
        this.timestamp = timestamp;
        this.message = message;
        this.details = details;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

我还有一个自定义异常处理程序:

@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
@RestController
public class CustomizedExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @Override
    public final ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request){
        ErrorDetails errorDetails = new ErrorDetails(new Date(), "hello",request.getDescription(true));
        errorDetails.setMessage("Testing message");
        return new ResponseEntity<>(errorDetails,HttpStatus.NOT_ACCEPTABLE);
    }
}

但是当我尝试发布错误请求时,例如,使用应该具有整数值的字段时,我在 JSON 中传递了一个字符串,它仍然返回以下默认错误消息:

{
    "timestamp": "2019-03-12T00:15:14.210+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "JSON parse error: Cannot deserialize value of type `int` from String \"lala\": not a valid Integer value; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `int` from String \"lala\": not a valid Integer value\n at [Source: (PushbackInputStream); line: 5, column: 17] (through reference chain: com.tdl.model.ToDoNote[\"priority\"])",
    "path": "/todos"
}

JSON 请求:

{
    "name": "An workout",
    "dateToComplete": "Today",
    "description": "Sleep Day",
    "priority": "lala",
    "completed": false
}

想要的效果只是出现测试消息而不是长描述。

我也在我的 Eclipse 控制台中得到了这个:

WARN 16508 --- [nio-5000-exec-4] .wsmsDefaultHandlerExceptionResolver:已解决 [org.springframework.http.converter.HttpMessageNotReadableException:JSON 解析错误:无法从字符串“lala”反序列化 int 类型的值:不是有效的整数值;嵌套异常是 com.fasterxml.jackson.databind.exc.InvalidFormatException:无法从字符串“lala”反序列化类型为 int 的值:不是有效的整数值 在 [来源:(PushbackInputStream); line: 5, column: 17] (通过引用链: com.tdl.model.ToDoNote["priority"])]

我将状态更改为 NOT_ACCEPTABLE 只是为了更清楚地查看是否返回了我的自定义错误。

任何帮助将不胜感激。谢谢。

编辑

为 InvalidFormatException 添加了 ExceptionHandler,但没有任何改变。我仍然收到与以前相同的默认错误(异常)消息。

@ExceptionHandler(InvalidFormatException.class)
    public final ResponseEntity<Object> handleInvalidFormat(InvalidFormatException ex, HttpHeaders headers, HttpStatus status, WebRequest request){
        ErrorDetails errorDetails = new ErrorDetails(new Date(), "hello",request.getDescription(true));
        errorDetails.setMessage("Testing message");
        return new ResponseEntity<>(errorDetails,HttpStatus.NOT_ACCEPTABLE);

    }

【问题讨论】:

  • 尝试InvalidFormatException异常
  • @Deadpool 试过了,但没有改变:/(现在有 handleHttpMessageNotReadable 和 InvalidFormatException ExceptionHandlers)

标签: java json spring spring-boot exception


【解决方案1】:

问题解决了。我将自定义异常类放在一个命名错误的包中。它被称为只是例外。虽然它应该是整个项目所在的 com.app.exception。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-31
    • 2019-09-22
    • 2017-11-01
    • 1970-01-01
    • 2018-07-17
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多