W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
在 Dubbo 中進(jìn)行參數(shù)驗(yàn)證
參數(shù)驗(yàn)證功能是基于 JSR303 實(shí)現(xiàn)的,用戶只需標(biāo)識(shí) JSR303 標(biāo)準(zhǔn)的驗(yàn)證 annotation,并通過聲明 filter 來實(shí)現(xiàn)驗(yàn)證。
<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) // 長(zhǎng)度或大小范圍
private String name;
@NotNull(groups = ValidationService.Save.class) // 保存時(shí)不允許為空,更新時(shí)允許為空 ,表示不更新該字段
@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 // 必須為一個(gè)過去的時(shí)間
private Date loginDate;
@Future // 必須為一個(gè)未來的時(shí)間
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 { // 缺省可按服務(wù)接口區(qū)分驗(yàn)證場(chǎng)景,如:@NotNull(groups = ValidationService.class)
@interface Save{} // 與方法同名接口,首字母大寫,用于區(qū)分驗(yàn)證場(chǎng)景,如:@NotNull(groups = ValidationService.Save.class),可選
void save(ValidationParameter parameter);
void update(ValidationParameter parameter);
}
import javax.validation.GroupSequence;
public interface ValidationService {
@GroupSequence(Update.class) // 同時(shí)驗(yàn)證Update組規(guī)則
@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); // 驗(yàn)證參數(shù)不為空
void delete(@Min(1) int id); // 直接對(duì)基本類型參數(shù)驗(yàn)證
}
<dubbo:reference id="validationService" interface="org.apache.dubbo.examples.validation.api.ValidationService" validation="true" />
<dubbo:service interface="org.apache.dubbo.examples.validation.api.ValidationService" ref="validationService" validation="true" />
提示
Dubbo 默認(rèn)支持 hibernate-validator 版本 <=6.x,若使用 hibernate-validator 7.x 版本,請(qǐng)將 validation 參數(shù)聲明為 jvalidatorNew
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");
// Error
try {
parameter = new ValidationParameter();
validationService.save(parameter);
System.out.println("Validation ERROR");
} catch (RpcException e) { // 拋出的是RpcException
ConstraintViolationException ve = (ConstraintViolationException) e.getCause(); // 里面嵌了一個(gè)ConstraintViolationException
Set<ConstraintViolation<?>> violations = ve.getConstraintViolations(); // 可以拿到一個(gè)驗(yàn)證錯(cuò)誤詳細(xì)信息的集合
System.out.println(violations);
}
}
}
提示
自 ?2.1.0
? 版本開始支持, 如何使用可以參考 dubbo 項(xiàng)目中的示例代碼
驗(yàn)證方式可擴(kuò)展,擴(kuò)展方式參見開發(fā)者手冊(cè)中的驗(yàn)證擴(kuò)展
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: