使用telnet命令调用dubbo接口报错如上图,下图是入参的实体类注解
实体类新增注解 @NoArgsConstructor, @AllArgsConstructor 再次调用成功
解析:
@AllArgsConstructor、@NoArgsConstructor、@Builder 注解是关于类的构造函数的。
1:@Bulider
会生成一个全参的构造方法,原本默认自带的无参构造方法不提供了
查看LklOSApplyOpenAccountRequest.class 可以清晰看到如下图所示:
public class LklOSApplyOpenAccountRequest {
private String userId;
private String userName;
private String mobile;
public boolean checkParam() {
ParamChecker.notBlank(this.userId, "店铺编号(userId)不能为空!");
ParamChecker.notBlank(this.userName, "店铺名称(userName)不能为空!");
ParamChecker.notBlank(this.mobile, "手机号(mobile)不能为空!");
return super.checkParam();
}
LklOSApplyOpenAccountRequest(String userId, String userName, String mobile) {
this.userId = userId;
this.userName = userName;
this.mobile = mobile;
}
public static LklOSApplyOpenAccountRequest.LklOSApplyOpenAccountRequestBuilder builder() {
return new LklOSApplyOpenAccountRequest.LklOSApplyOpenAccountRequestBuilder();
}
public String getUserId() {
return this.userId;
}
public String getUserName() {
return this.userName;
}
public String getMobile() {
return this.mobile;
}
public void setUserId(String userId) {
this.userId = userId;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public static class LklOSApplyOpenAccountRequestBuilder {
private String userId;
private String userName;
private String mobile;
LklOSApplyOpenAccountRequestBuilder() {
}
public LklOSApplyOpenAccountRequest.LklOSApplyOpenAccountRequestBuilder userId(String userId) {
this.userId = userId;
return this;
}
public LklOSApplyOpenAccountRequest.LklOSApplyOpenAccountRequestBuilder userName(String userName) {
this.userName = userName;
return this;
}
public LklOSApplyOpenAccountRequest.LklOSApplyOpenAccountRequestBuilder mobile(String mobile) {
this.mobile = mobile;
return this;
}
public LklOSApplyOpenAccountRequest build() {
return new LklOSApplyOpenAccountRequest(this.userId, this.userName, this.mobile);
}
public String toString() {
return "LklOSApplyOpenAccountRequest.LklOSApplyOpenAccountRequestBuilder(userId=" + this.userId + ", userName=" + this.userName + ", mobile=" + this.mobile + ")";
}
}
}
了解:
com.alibaba.dubbo.common.utils.PojoUtils
对象没有无参构造函数是,则调用时会报错:java.lang.RuntimeException: Illegal constructor:根据上面报错日志,查看到PojoUtils报错方法:
由于Class<?>[] empty = {} 是空,调用无参构造函数,使用@Bulider方法,会生成一个全参的构造方法,没有无参构造,必然会报错
2:@NoArgsConstructor 使用后创建一个无参构造函数
如果显式声明了@NoArgsConstructor,lombok就不会生成全参构造函数,而@Builder中会用到全参构造函数,所以会冲突
3:@AllArgsConstructor 全参构造注解
该构造函数含有所有已声明字段属性参数,增加@AllArgsConstructor 可以解决@Bulider和 @NoArgsConstructor的冲突
总结:联合使用
@Builder
@NoArgsConstructor
@AllArgsConstructor
使用上面注解之后,查看编译后的class ,可以看到无参构造+有参构造都存在,dubbo的invoke也正常调用