[英]Spring Boot request body validation add customize messages when input an invalid data type
I am using Spring Boot to create a POST request and I need to validate the request body based on user inputs.我正在使用 Spring Boot 创建 POST 请求,我需要根据用户输入验证请求正文。 However, when the user inputS an invalid data type, the response shows nothing, just
400 bad request
status.但是,当用户输入一个无效的数据类型时,响应什么也没有显示,只是
400 bad request
status。 Can I add a message to show the user which field is an invalid data type?我可以添加一条消息来向用户显示哪个字段是无效数据类型吗?
For example: Here is my controller:例如:这是我的 controller:
@RestController
@RequestMapping("/api/foo")
public class FooController {
@PostMapping("/save")
public void postFoo(@Valid @RequestBody Foo foo) {
// do somethings
}
}
And here is my Foo class:这是我的 Foo class:
public class Foo {
@NotBlank
private String name;
private Integer age;
// getter/setter
}
So now I post a request as below:所以现在我发布如下请求:
{
"name": "Foo Name",
"age": "A String"
}
The server will respond with the status 400 Bad request
without any message.服务器将响应状态
400 Bad request
而没有任何消息。 How can I put my message such as Age must be an integer
.我怎样才能把我的消息,例如
Age must be an integer
。
Until now I only have a solution that changes Age to String and adds a @Pattern validation annotation.到目前为止,我只有一个将 Age 更改为 String 并添加 @Pattern 验证注释的解决方案。
public class Foo {
@NotBlank
private String name;
@Pattern(regexp = "[0-9]*", message = "Age must be an intege")
private String age;
// getter/setter
}
In your post method signature, you can make use of Response Entity class to show some exception message that is to be returned to the user, along with some status code.在您的 post 方法签名中,您可以使用响应实体 class 来显示一些要返回给用户的异常消息以及一些状态代码。
You need to implement error handling mechanism.您需要实现错误处理机制。
In your error handler you need to catch all exceptions and return error response.在您的错误处理程序中,您需要捕获所有异常并返回错误响应。 Here is an example based on Controller level ExceptionHandling
下面是一个基于 Controller 级别 ExceptionHandling 的示例
public class FooController{
//...
@ResponseStatus(value=HttpStatus.BAD_REQUEST)
@ExceptionHandler({ CustomException1.class, CustomException2.class })
public ErrorResponse handleException() {
//
}
}
Here in ErrorResponse model you can set error code and message according to exception and via ResponseStatus you can assign http errro code在 ErrorResponse model 中,您可以根据异常设置错误代码和消息,通过 ResponseStatus 您可以分配 http 错误代码
However this his approach has a major drawback: The @ExceptionHandler annotated method is only active for that particular Controller, not globally for the entire application.然而,他的方法有一个主要缺点:@ExceptionHandler 注释方法仅对特定的 Controller 有效,而不是对整个应用程序全局有效。
To globally handle exception you can use ControllerAdvice.要全局处理异常,您可以使用 ControllerAdvice。 Here is a good article on overall error handling mechanism
这是一篇关于整体错误处理机制的好文章
Replacing @NotBlank
with @NotBlank(message = "Age must be an integer")
will solve your issue.用
@NotBlank
@NotBlank(message = "Age must be an integer")
替换 @NotBlank 将解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.