參數驗證功能 1 是基於 JSR303 實現的,用戶只需標識 JSR303 標準的驗證 annotation,並經過聲明 filter 來實現驗證 2。html
<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>
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; } }
public interface ValidationService { // 缺省可按服務接口區分驗證場景,如:@NotNull(groups = ValidationService.class) @interface Save{} // 與方法同名接口,首字母大寫,用於區分驗證場景,如:@NotNull(groups = ValidationService.Save.class),可選 void save(ValidationParameter parameter); void update(ValidationParameter parameter); }
import javax.validation.GroupSequence; public interface ValidationService { @GroupSequence(Update.class) // 同時驗證Update組規則 @interface Save{} void save(ValidationParameter parameter); @interface Update{} void update(ValidationParameter parameter); }
import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; public interface ValidationService { void save(@NotNull ValidationParameter parameter); // 驗證參數不爲空 void delete(@Min(1) int id); // 直接對基本類型參數驗證 }
<dubbo:reference id="validationService" interface="com.alibaba.dubbo.examples.validation.api.ValidationService" validation="true" />
<dubbo:service interface="com.alibaba.dubbo.examples.validation.api.ValidationService" ref="validationService" validation="true" />
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.alibaba.dubbo.examples.validation.api.ValidationParameter; import com.alibaba.dubbo.examples.validation.api.ValidationService; import com.alibaba.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"); // Error try { parameter = new ValidationParameter(); validationService.save(parameter); System.out.println("Validation ERROR"); } catch (RpcException e) { // 拋出的是RpcException ConstraintViolationException ve = (ConstraintViolationException) e.getCause(); // 裏面嵌了一個ConstraintViolationException Set<ConstraintViolation<?>> violations = ve.getConstraintViolations(); // 能夠拿到一個驗證錯誤詳細信息的集合 System.out.println(violations); } } }