有的时候博客内容会有变动,首发博客是最新的,其他博客地址可能会未同步, 认准 https://blog.zysicyj.top
全网最细面试题手册,支持艾宾浩斯记忆法。这是一份最全面、最详细、最高质量的 java 面试题,不建议你死记硬背,只要每天复习一遍,有个大概印象就行了。https://store.amazingmemo.com/chapterDetail/1685324709017001`
在 Dubbo 中进行参数验证
背景
参数验证功能是基于 JSR303 实现的,用户只需标识 JSR303 标准的验证 annotation,并通过声明 filter 来实现验证。
示例
Maven 依赖
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>1.0.0.GA</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency>
|
参数标注示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| import java.io.Serializable; import java.util.Date; import javax.validation.constraints.Future; import javax.validation.constraints.Max; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; import javax.validation.constraints.Past; import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; public class ValidationParameter implements Serializable { private static final long serialVersionUID = 7158911668568000392L; @NotNull @Size(min = 1, max = 20) private String name; @NotNull(groups = ValidationService.Save.class) @Pattern(regexp = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$") private String email; @Min(18) @Max(100) private int age; @Past private Date loginDate; @Future private Date expiryDate; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getLoginDate() { return loginDate; } public void setLoginDate(Date loginDate) { this.loginDate = loginDate; } public Date getExpiryDate() { return expiryDate; } public void setExpiryDate(Date expiryDate) { this.expiryDate = expiryDate; } }
|
分组验证示例
1 2 3 4 5 6
| public interface ValidationService { @interface Save{} void save(ValidationParameter parameter); void update(ValidationParameter parameter); }
|
关联验证示例
1 2 3 4 5 6 7 8 9 10 11
| import javax.validation.GroupSequence; public interface ValidationService { @GroupSequence(Update.class) @interface Save{} void save(ValidationParameter parameter); @interface Update{} void update(ValidationParameter parameter); }
|
参数验证示例
1 2 3 4 5 6 7 8
| import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; public interface ValidationService { void save(@NotNull ValidationParameter parameter); void delete(@Min(1) int id); }
|
配置
在客户端验证参数
1 2
| <dubbo:reference id="validationService" interface="org.apache.dubbo.examples.validation.api.ValidationService" validation="true" />
|
在服务器端验证参数
1 2
| <dubbo:service interface="org.apache.dubbo.examples.validation.api.ValidationService" ref="validationService" validation="true" />
|
验证异常信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.apache.dubbo.examples.validation.api.ValidationParameter; import org.apache.dubbo.examples.validation.api.ValidationService; import org.apache.dubbo.rpc.RpcException; public class ValidationConsumer { public static void main(String[] args) throws Exception { String config = ValidationConsumer.class.getPackage().getName().replace('.', '/') + "/validation-consumer.xml"; ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(config); context.start(); ValidationService validationService = (ValidationService)context.getBean("validationService"); try { parameter = new ValidationParameter(); validationService.save(parameter); System.out.println("Validation ERROR"); } catch (RpcException e) { ConstraintViolationException ve = (ConstraintViolationException) e.getCause(); Set<ConstraintViolation<?>> violations = ve.getConstraintViolations(); System.out.println(violations); } } }
|