网站做微信支付对接,免费个人简历表,新风格网站,嘉定区建设局网站在 Java 开发中#xff0c;数据校验是确保应用程序的数据完整性和一致性的重要步骤。Java 提供了一系列注解来简化数据校验的过程#xff0c;以下是一些常用的字段校验注解及其示例代码#xff1a;
NotNull NotNull 用于确保字段不为 null#xff0c;适用于任何类型的字段…在 Java 开发中数据校验是确保应用程序的数据完整性和一致性的重要步骤。Java 提供了一系列注解来简化数据校验的过程以下是一些常用的字段校验注解及其示例代码
NotNull NotNull 用于确保字段不为 null适用于任何类型的字段包括基本数据类型和对象引用。 import javax.validation.constraints.NotNull;public class Example {NotNull(message 字段不能为空)private String name;// Getters and setters
}NotBlank NotBlank 用于确保字符串字段不为空且长度大于 0仅适用于字符串类型的字段。 import javax.validation.constraints.NotBlank;public class Example {NotBlank(message 姓名不能为空或者空字符串)private String name;// Getters and setters
}NotEmpty NotEmpty 用于确保集合、数组、Map 或者字符串类型的字段不为空。 import javax.validation.constraints.NotEmpty;
import java.util.List;public class Example {NotEmpty(message 列表不能为空且至少包含一个元素)private ListString items;// Getters and setters
}Min 和 Max Min 和 Max 用于确保数字字段的值在指定的范围内。 import javax.validation.constraints.Min;
import javax.validation.constraints.Max;public class Example {Min(value 18, message 年龄不能小于18岁)Max(value 100, message 年龄不能大于100岁)private int age;// Getters and setters
}Size Size 用于确保集合、数组或者字符串字段的大小在指定范围内。
复制代码
import javax.validation.constraints.Size;
import java.util.List;public class Example {Size(min 2, max 50, message 用户名长度必须在2到50之间)private String username;Size(min 1, max 10, message 列表大小必须在1到10之间)private ListString items;Size(min 1, max 100, message 数组长度必须在1到100之间)private String[] array;// Getters and setters
}Pattern Pattern 使用正则表达式验证字符串字段的格式。 import javax.validation.constraints.Pattern;public class Example {Pattern(regexp [a-zA-Z0-9], message 用户名只能包含字母和数字)private String username;// Getters and setters
}Valid Valid 用于嵌套验证对一个对象的属性进行验证。
复制代码
import javax.validation.Valid;public class Outer {Validprivate Inner inner;// Getters and setters
}public class Inner {NotBlank(message 姓名不能为空)private String name;// Getters and setters
}控制器Controller中启用字段校验 在 Spring Boot 中可以通过在控制器类或方法上添加 Validated 注解来启用验证并使用 Valid 注解对请求对象进行验证。 import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;import javax.validation.Valid;RestController
Validated // 注解表示启用验证
public class MyController {PostMapping(/example)public String example(RequestBody Valid MyRequest request) {// 处理请求return Success;}
}处理参数异常 在 Spring Boot 中可以通过使用 RestControllerAdvice 和 ExceptionHandler 注解来捕获验证错误并处理它们。 import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;import java.util.HashMap;
import java.util.Map;RestControllerAdvice
public class GlobalExceptionHandler {ExceptionHandler(MethodArgumentNotValidException.class)public MapString, String handleValidationExceptions(MethodArgumentNotValidException ex) {BindingResult result ex.getBindingResult();MapString, String errors new HashMap();for (FieldError error : result.getFieldErrors()) {errors.put(error.getField(), error.getDefaultMessage());}return errors;}
}通过使用这些注解可以有效地简化 Java 开发中的数据校验工作确保应用程序的数据完整性和一致性。希望这些示例代码能帮助你更好地理解它们的用法和作用。