dubbo接口入?yún)⑿r灧绞?validation)
更新時間:2026年02月06日 15:17:40 作者:C_Knight
本文詳細介紹了如何在Maven項目中配置Dubbo服務(wù)的全局Filter,使用com.alibaba.dubbo.rpc.Filter攔截器進行參數(shù)校驗,具體步驟包括接口入?yún)⑴渲?、服?wù)端遠程參數(shù)校驗和消費者本地參數(shù)校驗,確保數(shù)據(jù)傳輸和處理過程中的參數(shù)正確性和安全性
maven依賴
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
接口入?yún)?/h2>
@Data
public class UpdateBizLimitReqDTO {
private static final long serialVersionUID = 3217106310804365294L;
@NotNull(
message = "UpdateBizLimitReqDTO.ticket not null"
)
private String ticket;
@NotNull(
message = "UpdateBizLimitReqDTO.bizId not null"
)
private Long bizId;
@NotNull(
message = "UpdateBizLimitReqDTO.source not null"
)
private Integer source;
@NotNull(
message = "UpdateBizLimitReqDTO.limitItemList not null"
)
}
@Data
public class UpdateBizLimitReqDTO {
private static final long serialVersionUID = 3217106310804365294L;
@NotNull(
message = "UpdateBizLimitReqDTO.ticket not null"
)
private String ticket;
@NotNull(
message = "UpdateBizLimitReqDTO.bizId not null"
)
private Long bizId;
@NotNull(
message = "UpdateBizLimitReqDTO.source not null"
)
private Integer source;
@NotNull(
message = "UpdateBizLimitReqDTO.limitItemList not null"
)
}
配置全局Filter
- 配置com.alibaba.dubbo.rpc.Filter文件
exceptionResolver=com.*.*.MPExceptionReslover

攔截器代碼實現(xiàn)。
/**
* 統(tǒng)一異常處理,如果存在沒有catch住的異常,統(tǒng)一在此處封裝成默認未知錯誤
*/
@Slf4j
@Activate(
group = {"provider", "consumer"}
)
public class MPExceptionReslover implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) {
try{
Object [] args = invocation.getArguments();
if(args != null && args.length != 0){
for (int i = 0; i < args.length; i++) {
if(args[i] == null){
throw new BizValidatorException("請求參數(shù)不能為空");
}
RequestValidator validator = ValidatorFactory.getValidator(args[i].getClass());
if(validator != null){
validator.doValidate(args[i]);
}
}
}
Result result = invoker.invoke(invocation);
Throwable exception = result.getException();
if (exception != null) {
return wrapException(result.getException());
} else {
return result;
}
}catch (Exception e){
return wrapException(e);
}
}
private RpcResult wrapException(Throwable exception) {
if(exception instanceof BizException){
BizException bizException = (BizException) exception;
return new RpcResult(soaResponse(bizException.getErrorCode(), bizException.getErrorMsg()));
}else if(exception instanceof RpcException){
//入?yún)⑿r?
RpcException rpcException = (RpcException)exception;
if(rpcException.getCause() instanceof ConstraintViolationException){
ConstraintViolationException violationException = (ConstraintViolationException)rpcException.getCause();
Set<ConstraintViolation<?>> errReason = violationException.getConstraintViolations();
for (ConstraintViolation<?> item : errReason) {
return new RpcResult(soaResponse(PromotionErrorCode.FAIL_PARAM_NULL.getErrorCode(), item.getMessageTemplate()));
}
}
}
return new RpcResult(soaResponse("999999", exception.getMessage()));
}
/**
* 構(gòu)建SoaResponse對象
*/
private SoaResponse<Void, Void> soaResponse(String returnCode, String errorMsg) {
SoaResponse<Void, Void> soaResponse = new SoaResponse<>();
soaResponse.setResponseVo(null);
soaResponse.setErrT(null);
soaResponse.setReturnCode(returnCode);
soaResponse.setReturnMsg(errorMsg);
soaResponse.setProcessResult(Boolean.FALSE);
return soaResponse;
}
}
dubbo開啟入?yún)⑿r?/h2>
- 服務(wù)端遠程參數(shù)校驗
<dubbo:provider validation="true"/>
- 消費者本地參數(shù)校驗
<dubbo:consumer validation="true"/>
<dubbo:provider validation="true"/>
<dubbo:consumer validation="true"/>
總結(jié)以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot?Admin集成診斷利器Arthas示例實現(xiàn)
這篇文章主要為大家介紹了SpringBoot?Admin集成診斷利器Arthas示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Spring Security Oauth2.0認證授權(quán)教程
Spring Security實現(xiàn)用戶認證、會話管理及授權(quán),支持Token等多方式,OAuth2.0用于分布式系統(tǒng)統(tǒng)一認證,網(wǎng)關(guān)解析令牌并轉(zhuǎn)發(fā)請求2025-07-07
java通過ssh連接執(zhí)行shell命令,文件傳輸方式
這篇文章主要介紹了java通過ssh連接執(zhí)行shell命令,文件傳輸方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
java線程安全鎖ReentrantReadWriteLock原理分析readLock
這篇文章主要為大家介紹了java線程安全鎖ReentrantReadWriteLock原理分析readLock,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Java Spring @Autowired的這些騷操作,你都知道嗎
這篇文章主要介紹了徹底搞明白Spring中的自動裝配和Autowired注解的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-09-09
利用EasyExcel導(dǎo)出帶有選擇校驗框的excel
EasyExcel是一個輕量級的Excel處理工具,支持Excel?2003(xls)和Excel?2007及以上版本(xlsx)的文件格式,本文將利用EasyExcel導(dǎo)出帶有選擇校驗框的excel,需要的可以參考下2024-12-12

