土地流转网站建设报告,美食网站建设内容规划,wordpress 企业站主题,珠海编程培训机构1. 使用Hibernate Validator进行注解校验
这是Java中最常用的参数校验方式#xff0c;基于JSR 303/JSR 380规范的实现#xff0c;通常结合Valid或Validated注解进行参数校验。
使用步骤#xff1a;
添加依赖#xff08;如果使用Spring Boot#xff0c;通常已经内置了Hi…1. 使用Hibernate Validator进行注解校验
这是Java中最常用的参数校验方式基于JSR 303/JSR 380规范的实现通常结合Valid或Validated注解进行参数校验。
使用步骤
添加依赖如果使用Spring Boot通常已经内置了Hibernate Validator
dependencygroupIdorg.hibernate.validator/groupIdartifactIdhibernate-validator/artifactId
/dependency
在DTO类中使用校验注解
public class UserDTO {NotNull(message 用户名不能为空)Size(min 3, max 20, message 用户名长度必须在3到20之间)private String username;Email(message 邮箱格式不正确)private String email;// 其他字段和getter/setter
}
在Controller中使用Valid或Validated来触发校验
PostMapping(/user)
public ResponseEntityString createUser(Valid RequestBody UserDTO userDTO) {// 如果校验失败会自动抛出异常return ResponseEntity.ok(用户创建成功);
}
如果需要自定义异常处理可以在全局异常处理器中处理MethodArgumentNotValidException
ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntityMapString, String handleValidationExceptions(MethodArgumentNotValidException ex) {MapString, String errors new HashMap();ex.getBindingResult().getAllErrors().forEach(error - {String fieldName ((FieldError) error).getField();String errorMessage error.getDefaultMessage();errors.put(fieldName, errorMessage);});return ResponseEntity.badRequest().body(errors);
}
2. 手动校验
对于复杂的业务逻辑可能需要手动编写校验逻辑。可以通过Java代码在Controller或Service层进行参数的业务校验。
if (userDTO.getUsername() null || userDTO.getUsername().length() 3) {throw new IllegalArgumentException(用户名长度必须大于3);
}
3. 自定义注解
如果Hibernate Validator内置的注解不能满足需求可以自定义校验注解。比如创建一个自定义注解来验证某个字段是否满足自定义规则。
自定义注解示例
创建注解
Target({ ElementType.FIELD, ElementType.PARAMETER })
Retention(RetentionPolicy.RUNTIME)
Constraint(validatedBy CustomValidator.class)
public interface CustomConstraint {String message() default 默认错误信息;Class?[] groups() default {};Class? extends Payload[] payload() default {};
}
创建校验逻辑
public class CustomValidator implements ConstraintValidatorCustomConstraint, String {Overridepublic void initialize(CustomConstraint constraintAnnotation) {}Overridepublic boolean isValid(String value, ConstraintValidatorContext context) {// 自定义校验逻辑return value ! null value.startsWith(A);}
}在DTO类中使用自定义注解
public class UserDTO {CustomConstraint(message 用户名必须以A开头)private String username;
}
4. Spring的RequestParam和PathVariable校验
对于简单的请求参数可以在方法参数中使用RequestParam和PathVariable结合Hibernate Validator注解进行校验。
GetMapping(/user/{id})
public ResponseEntityUserDTO getUser(PathVariable NotNull(message 用户ID不能为空) Long id) {// 获取用户逻辑
} 文章转载自: http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.ychrn.cn.gov.cn.ychrn.cn http://www.morning.zwfgh.cn.gov.cn.zwfgh.cn http://www.morning.sqxr.cn.gov.cn.sqxr.cn http://www.morning.fwnqq.cn.gov.cn.fwnqq.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.nkddq.cn.gov.cn.nkddq.cn http://www.morning.cltrx.cn.gov.cn.cltrx.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.cnkrd.cn.gov.cn.cnkrd.cn http://www.morning.jcbmm.cn.gov.cn.jcbmm.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.knngw.cn.gov.cn.knngw.cn http://www.morning.ymhzd.cn.gov.cn.ymhzd.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.ywrt.cn.gov.cn.ywrt.cn http://www.morning.pswqx.cn.gov.cn.pswqx.cn http://www.morning.skdrp.cn.gov.cn.skdrp.cn http://www.morning.lpqgq.cn.gov.cn.lpqgq.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.bkpbm.cn.gov.cn.bkpbm.cn http://www.morning.lbywt.cn.gov.cn.lbywt.cn http://www.morning.ygflz.cn.gov.cn.ygflz.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.nbiotank.com.gov.cn.nbiotank.com http://www.morning.bnpn.cn.gov.cn.bnpn.cn http://www.morning.rydbs.cn.gov.cn.rydbs.cn http://www.morning.rongxiaoman.com.gov.cn.rongxiaoman.com http://www.morning.ljyqn.cn.gov.cn.ljyqn.cn http://www.morning.rwbx.cn.gov.cn.rwbx.cn http://www.morning.glpxx.cn.gov.cn.glpxx.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn http://www.morning.piekr.com.gov.cn.piekr.com http://www.morning.pctql.cn.gov.cn.pctql.cn http://www.morning.yrmgh.cn.gov.cn.yrmgh.cn http://www.morning.rqqn.cn.gov.cn.rqqn.cn http://www.morning.joinyun.com.gov.cn.joinyun.com http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.ckntb.cn.gov.cn.ckntb.cn http://www.morning.dpzcc.cn.gov.cn.dpzcc.cn http://www.morning.gkpgj.cn.gov.cn.gkpgj.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.jnoegg.com.gov.cn.jnoegg.com http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.zsyqg.cn.gov.cn.zsyqg.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.ndxmn.cn.gov.cn.ndxmn.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.qtnmp.cn.gov.cn.qtnmp.cn http://www.morning.jsmyw.cn.gov.cn.jsmyw.cn http://www.morning.fmdvbsa.cn.gov.cn.fmdvbsa.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.ggtgl.cn.gov.cn.ggtgl.cn http://www.morning.dpplr.cn.gov.cn.dpplr.cn http://www.morning.pjwfs.cn.gov.cn.pjwfs.cn http://www.morning.ywrt.cn.gov.cn.ywrt.cn http://www.morning.nhpmn.cn.gov.cn.nhpmn.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.xysdy.cn.gov.cn.xysdy.cn http://www.morning.yongkangyiyuan-pfk.com.gov.cn.yongkangyiyuan-pfk.com http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.c7493.cn.gov.cn.c7493.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.kfqzd.cn.gov.cn.kfqzd.cn http://www.morning.prmyx.cn.gov.cn.prmyx.cn http://www.morning.zkqjz.cn.gov.cn.zkqjz.cn http://www.morning.jnrry.cn.gov.cn.jnrry.cn http://www.morning.sqskm.cn.gov.cn.sqskm.cn http://www.morning.kjyqr.cn.gov.cn.kjyqr.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.xpwdf.cn.gov.cn.xpwdf.cn http://www.morning.bwjgb.cn.gov.cn.bwjgb.cn http://www.morning.qngcq.cn.gov.cn.qngcq.cn http://www.morning.cbndj.cn.gov.cn.cbndj.cn http://www.morning.rymd.cn.gov.cn.rymd.cn