dubbo接口入?yún)⑿r?yàn)方式(validation)
更新時(shí)間:2026年02月06日 15:17:40 作者:C_Knight
本文詳細(xì)介紹了如何在Maven項(xiàng)目中配置Dubbo服務(wù)的全局Filter,使用com.alibaba.dubbo.rpc.Filter攔截器進(jìn)行參數(shù)校驗(yàn),具體步驟包括接口入?yún)⑴渲谩⒎?wù)端遠(yuǎn)程參數(shù)校驗(yàn)和消費(fèi)者本地參數(shù)校驗(yàn),確保數(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

攔截器代碼實(shí)現(xiàn)。
/**
* 統(tǒng)一異常處理,如果存在沒有catch住的異常,統(tǒng)一在此處封裝成默認(rèn)未知錯(cuò)誤
*/
@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("請(qǐng)求參數(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?yàn)
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對(duì)象
*/
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?yàn)
- 服務(wù)端遠(yuǎn)程參數(shù)校驗(yàn)
<dubbo:provider validation="true"/>
- 消費(fèi)者本地參數(shù)校驗(yàn)
<dubbo:consumer validation="true"/>
總結(jié)以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端的操作方法
這篇文章主要介紹了通過netty把百度地圖API獲取的地理位置從Android端發(fā)送到Java服務(wù)器端,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-10-10
SpringBoot使用阿里OSS實(shí)現(xiàn)文件云存儲(chǔ)的方法
這篇文章主要介紹了SpringBoot使用阿里OSS實(shí)現(xiàn)文件云存儲(chǔ),本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
spring boot集成rabbitmq的實(shí)例教程
這篇文章主要給大家介紹了關(guān)于spring boot集成rabbitmq的相關(guān)資料,springboot集成RabbitMQ非常簡(jiǎn)單,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
java啟動(dòng)時(shí)自定義配置文件路徑,自定義log4j2.xml位置方式
這篇文章主要介紹了java啟動(dòng)時(shí)自定義配置文件路徑,自定義log4j2.xml位置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Jmeter的接口測(cè)試詳細(xì)步驟并實(shí)現(xiàn)業(yè)務(wù)閉環(huán)
這篇文章主要介紹了Jmeter的接口測(cè)試詳細(xì)步驟并實(shí)現(xiàn)業(yè)務(wù)閉環(huán),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-08-08
配置化Feign接口動(dòng)態(tài)切換URL方式
本文介紹了在開發(fā)、測(cè)試和生產(chǎn)環(huán)境中使用Feign接口時(shí),根據(jù)不同的環(huán)境動(dòng)態(tài)切換調(diào)用URL的方法,通過在不同環(huán)境的配置文件中配置URL,并實(shí)現(xiàn)一個(gè)Feign攔截器來讀取這些配置,從而實(shí)現(xiàn)URL的動(dòng)態(tài)切換,這種方法避免了引入過多步驟,同時(shí)也保證了不同環(huán)境下的URL正確調(diào)用2024-11-11

