spring—当请求体未能通过使用bean验证/hibernate验证程序定义的验证时,如何返回自定义响应pojo?

zte4gxcn  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(363)

是否可以重写spring hibernate验证器的默认响应pojo?
当前,当验证失败时,一个非常大的响应返回给客户端,如下所示。但是我不希望客户机提供hibernate验证器的完整错误响应,而是发送一些关于错误消息的键值对。

{
    "timestamp": "2018-05-28T18:12:56.705+0000",
    "status": 400,
    "error": "Bad Request",
    "errors": [
        {
            "codes": [
                "NotBlank.abc.xyz",
                "NotBlank.xyz",
                "NotBlank.java.lang.String",
                "NotBlank"
            ],
            "arguments": [
                {
                    "codes": [
                        "abc.xyz",
                        "xyz"
                    ],
                    "arguments": null,
                    "defaultMessage": "transactionId",
                    "code": "transactionId"
                }
            ],
            "defaultMessage": "xyz is mandatory parameter , please provide appropriate value",
            "objectName": "abc",
            "field": "xyz",
            "rejectedValue": "",
            "bindingFailure": false,
            "code": "NotBlank"
        }
    ],
    "message": "Validation failed for object='xyz'. Error count: 1",
    "path": "/path/create/1"
}
dgsult0t

dgsult0t1#

BindException 当请求主体验证失败时抛出。你可以自己定义 ControllerAdvice 从中的详细信息构造适当的错误消息 BindException .

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import com.example.demo.ErrorResponse.ErrorDetails;

@ControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(BindException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public ErrorResponse handleException(BindException ex) {

        List<FieldError> errors = ex.getBindingResult().getFieldErrors();

        List<ErrorDetails> errorDetails = new ArrayList<>();
        for (FieldError fieldError : errors) {
            ErrorDetails error = new ErrorDetails();
            error.setFieldName(fieldError.getField());
            error.setMessage(fieldError.getDefaultMessage());
            errorDetails.add(error);
        }

        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setErrors(errorDetails);

        return errorResponse;
    }

}

错误响应的pojo:

@Data
public class ErrorResponse {

    private List<ErrorDetails> errors;

    @Data
    public static class ErrorDetails {
        private String fieldName;
        private String message;
    }
}

错误消息示例

{
    "errors": [
        {
            "fieldName": "firstName",
            "message": "must not be null"
        },
        {
            "fieldName": "lastName",
            "message": "must not be null"
        }
    ]
}

相关问题