Java@NotBlank
时间: 2024-05-02 11:20:27 浏览: 86
Java @NotBlank is a validation constraint used in Java programming language to check if a given string is not blank or empty. It is a part of the Java Bean Validation API and is commonly used to validate user inputs in web applications.
To use the @NotBlank constraint, you need to add the following import statement to your Java class:
```java
import javax.validation.constraints.NotBlank;
```
Then, you can annotate your string field or parameter with the @NotBlank constraint as follows:
```java
public class User {
@NotBlank(message = "Username cannot be blank")
private String username;
public User(String username) {
this.username = username;
}
// ...
}
```
In this example, the @NotBlank constraint is used to ensure that the username field is not blank or empty. If the username is blank, a validation error message "Username cannot be blank" will be returned.
Note that the @NotBlank constraint only checks for blank or empty strings, not for null values. If you need to check for null values as well, you can use the @NotNull constraint.
阅读全文