猿问

无法使用自定义 HttpMessageNotReadableSception

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


我有一个错误详细信息类:


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);

    }

}


慕哥6287543
浏览 297回答 2
2回答

慕工程0101907

我遇到了这个错误HttpMessageNotReadableException,我觉得有必要自定义它。经过几次试验,我最终得到了一种更好,更易读的格式。步骤 1:使用要向客户端公开的字段创建自定义错误详细信息类。以下是我创建的内容。public class ErrorDetails {private final Date timestamp;private final String message;private final String details;public ErrorDetails(Date timestamp, String message, String details) {&nbsp; &nbsp; this.timestamp = timestamp;&nbsp; &nbsp; this.message = message;&nbsp; &nbsp; this.details=details;}为简洁起见,不包括 getters步骤 2:创建一个将扩展响应实体处理程序的类,该处理程序具有可以重写的异常。在这里,重写 handleHttpMessageNotReadbale 方法,然后在该方法中实现你自己的自定义错误处理程序。@ControllerAdvice公共类 GlobalExceptionHandler 扩展了 ResponseEntityExceptionHandler {@Overrideprotected ResponseEntity<Object> handleHttpMessageNotReadable(&nbsp; &nbsp; &nbsp; &nbsp; HttpMessageNotReadableException ex,&nbsp; &nbsp; &nbsp; &nbsp; HttpHeaders headers,&nbsp; &nbsp; &nbsp; &nbsp; HttpStatus status,&nbsp; &nbsp; &nbsp; &nbsp; WebRequest request) {&nbsp; &nbsp; ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(),request.getDescription(false));&nbsp; &nbsp; return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);}}步骤3:使用错误的输入字段运行POST或PUT方法并检查结果。例如,性别是一个只有女性和男性的枚举类。{"firstName":"Dell","lastName":"HP","birthYear":"2000-02-12","email":"dell@gmail.com","gender":"BOY"}响应如下:{ “时间戳”: “2022-06-06T08:08:53.906+00:00”, “消息”: “JSON 解析错误: 无法反序列化字符串 ”BOY“ 中的类型值: 不是枚举类接受的值之一: [FEMALE, MALE];嵌套的例外是 com.fasterxml.jackson.databind.exc.InvalidFormatException: 无法反序列化字符串 “BOY” 中的类型值:不是 Enum 类接受的值之一: [FEMALE, MALE]\n at [Source: (org.springframework.util.StreamUtils$NonClosingInputStream); 行: 6, 列: 14] (通过引用链: com.io.clinic.payloadDTO.PatientDTO[“gender”])“, ”details“: ”uri=/api/v1/patients“ } 我对将消息置于该状态进行调试感到满意,但您也可以自定义 重写方法中的消息响应。com.io.clinic.utils.Gendercom.io.clinic.utils.Gender

白衣染霜花

问题解决了。我在一个名称不正确的包中有我的自定义异常类。它被称为例外。虽然它应该是com.app.exception,整个项目都在。
随时随地看视频慕课网APP

相关分类

Java
我要回答