@NotBlank报错是什么异常,如何获取message
时间: 2024-02-11 07:06:43 浏览: 29
@NotBlank是javax.validation.constraints包下的注解,用于验证字符串非空。当被注解的字符串为空时,会抛出ConstraintViolationException异常,其中包含了多个ConstraintViolation对象,每个对象都包含了发生异常的类、属性、错误信息等。
要获取@NotBlank抛出的异常信息,可以通过以下方式获取:
```java
try {
// 执行验证操作
} catch (ConstraintViolationException e) {
// 获取ConstraintViolationException中的所有异常信息
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
for (ConstraintViolation<?> violation : violations) {
// 获取异常信息
String message = violation.getMessage();
System.out.println(message);
}
}
```
其中,violation.getMessage()可以获取到@NotBlank注解中定义的错误信息,例如:不能为空。
相关问题
@NotBlank异常MethodArgumentNotValidException中如何获取message
当使用@NotBlank注解进行参数验证时,如果验证失败,则会抛出MethodArgumentNotValidException异常。这个异常中也包含了所有的验证错误信息,每个错误信息中都包含了相应的message信息。
可以在catch块中捕获这个异常,并通过异常对象的getBindingResult()方法获取到所有的验证错误信息,每个错误信息中都包含了相应的message信息。
以下是一个简单的示例代码:
```
@PostMapping("/users")
public void createUser(@RequestBody @Valid User user) {
// 创建用户
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public void handleValidationException(MethodArgumentNotValidException ex) {
// 获取所有验证错误信息
BindingResult bindingResult = ex.getBindingResult();
List<FieldError> errors = bindingResult.getFieldErrors();
for (FieldError error : errors) {
// 输出错误提示信息
System.out.println(error.getDefaultMessage());
}
}
```
在这个例子中,当使用@Valid注解对User对象进行验证时,如果验证失败,则会抛出MethodArgumentNotValidException异常。在异常处理方法中,通过getBindingResult()方法获取到所有的验证错误信息,并遍历每个错误信息输出相应的message信息。
@NotBlank异常中如何获取message
如果在使用@NotBlank注解时,传入了message参数,并且在验证时出现了空值或空格,会抛出ConstraintViolationException异常,并且异常信息中会包含message信息。
可以在catch块中捕获这个异常,并通过异常对象的getConstraintViolations()方法获取到所有的验证错误信息,每个错误信息中都包含了相应的message信息。
以下是一个简单的示例代码:
```
try {
// 使用@NotBlank注解进行验证
@NotBlank(message = "用户名不能为空")
String username = null;
} catch (ConstraintViolationException ex) {
// 获取所有验证错误信息
Set<ConstraintViolation<?>> violations = ex.getConstraintViolations();
for (ConstraintViolation<?> violation : violations) {
// 输出错误提示信息
System.out.println(violation.getMessage());
}
}
```
在这个例子中,由于username的值为null,所以会抛出ConstraintViolationException异常,并且异常信息中包含了"用户名不能为空"这个message信息。通过遍历异常对象中的ConstraintViolation对象,就可以获取到这个message信息并进行处理。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)