Dubbo RPC接口的參數(shù)校驗過程
Dubbo RPC接口的參數(shù)校驗
服務(wù)A調(diào)用服務(wù)B時,調(diào)用時即對服務(wù)B的接口參數(shù)進(jìn)行校驗 ,無需進(jìn)入到服務(wù)B
重點就是添加 validation = "true"
1、pom文件
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.1.Final</version>
</dependency>2、dubbo xml配置
或者Config類修改 或者@Reference時添加
在客戶端驗證參數(shù)
<dubbo:reference id="validationService" interface="com.alibaba.dubbo.examples.validation.api.ValidationService" validation="true" />
在服務(wù)器端驗證參數(shù)
<dubbo:service interface="com.alibaba.dubbo.examples.validation.api.ValidationService" validation="true" />
config類
@Bean
public ReferenceBean<PLRepaymentPlanService> repaymentPlanServiceReferenceBean(){
ReferenceBean<PLRepaymentPlanService> referenceBean = new ReferenceBean<>();
referenceBean.setInterface(PLRepaymentPlanService.class);
referenceBean.setCheck(false);
referenceBean.setValidation("true");
return referenceBean;
}@Reference
@Reference(validation = "true") TestService testService;
3、服務(wù)B的接口
Integer createRepaymentPlan(@Valid RepaymentPlanRequestDTO dto,@NotNull Integer age) throws PostLoanException;
DTO 內(nèi)容
@Data
public class RepaymentPlanRequestDTO implements Serializable {
/**
* 業(yè)務(wù)源
**/
@NotNull
private Integer businessSource;
/**
* 業(yè)務(wù)類型0,消費貸; 1,現(xiàn)金貸; 2,租房; 3:payday;4:無預(yù)約現(xiàn)金貸 5有錢貸,6指尖貸,7多享貸,8租機(jī),9白條,10醫(yī)美,11前置扣款
**/
@NotNull
private Integer type;
/**
* 業(yè)務(wù)規(guī)則
*/
private String businessRule;
/**
* 訂單id
**/
@NotNull
private Integer orderId;
/**
* 期數(shù)
**/
@NotNull
private Integer period;
/**
* 應(yīng)還總額
**/
@NotNull
private BigDecimal total;
/**
* 每期應(yīng)還
**/
@NotNull
private BigDecimal every;
/**
* 本金
**/
@NotNull
private BigDecimal principal;
/**
* 每期利率
**/
@NotNull
private BigDecimal rateEvery;
/**
* 服務(wù)費
**/
private BigDecimal serviceFee;
/**
* 用戶id
**/
@NotNull
private Integer userinfoId;
/**
* 商品id
**/
private Integer goodsId;
/**
* 渠道id
**/
private Integer channelId;
/**
* 資方id
**/
private Integer capitalId;
/**
* 起息日
*/
private LocalDateTime startInterestDate;
/**
* 貸款天數(shù)
**/
private Integer payDays;
/**
* 提單編號(規(guī)則O2O+訂單編號+資方+重試次數(shù))
*/
private String channelOrderNO;
/**
* 是否通知
* 需要資方生成還款計劃后資方平臺通知/回調(diào)的訂單
*/
private boolean hasNotified;
/**
* 訂單四要素
*/
@Valid
private PLUserInfoDTO plUserInfoDTO;
}可以嵌套校驗 但需要在嵌套校驗的字段上加@Valid
實現(xiàn)類上需要加@Validated
啟動服務(wù)B,服務(wù)A調(diào)用接口,符合添加的valid校驗標(biāo)準(zhǔn)
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
用Java實現(xiàn)春聯(lián)?支持自定義字體顏色
大家好,本篇文章主要講的是用Java編寫春聯(lián)?支持自定義字體顏色,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
理解 MyBatis 是如何在 Spring 容器中初始化的
這篇文章主要介紹了理解 MyBatis 是如何在 Spring 容器中初始化的,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
MyBatis 如何配置多個別名 typeAliasesPackage
這篇文章主要介紹了MyBatis 如何配置多個別名 typeAliasesPackage,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
SpringBoot微服務(wù)實現(xiàn)秒殺搶購代金券功能
本文詳細(xì)介紹了如何設(shè)計一個秒殺系統(tǒng),包括數(shù)據(jù)庫表設(shè)計、秒殺服務(wù)創(chuàng)建、使用限流、緩存和異步處理來應(yīng)對高并發(fā)挑戰(zhàn),感興趣的可以了解一下2025-09-09
利用Java工具類Hutool實現(xiàn)驗證碼校驗功能
這篇文章主要介紹了利用Java工具類Hutool實現(xiàn)驗證碼校驗功能,利用Hutool實現(xiàn)驗證碼校驗,校驗的Servlet與今天的第一篇是一樣的,唯一就是驗證碼的生成是不一樣的,利用Hutool生成驗證碼更快捷.需要的朋友可以參考下2022-10-10

