stacktrace: java.lang.ClassCastException: java.util.HashMap cannot be cast to

时间:2024-03-14 09:18:38

今天线上发了好几封预警邮件,邮件内容如下:

stacktrace: java.lang.ClassCastException: java.util.HashMap cannot be cast to

看了下项目报错的代码:

        ApiResult<WithdrawResultDto> withdrawResult = loanApiService.queryWithdrawResult(contractNo);
        WithdrawResultDto withdraw = withdrawResult.getData();

ApiResult 的代码:

public class ApiResult<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private boolean success;
    private String code;
    private String message;
    private String requestId;
    private T data;

    private ApiResult() {
    }
 }

发现逻辑上是完全没有问题的,报错是第二行代码,又看了下日志,终于发现了问题的原因:

[NettyClientWorker-thread-13{New I/O client worker #1-13}]  |-|WARN  |-|2018-12-19T16:42:37.938+08:00  |-|c.a.c.c.h.i.SerializerFactory[652]  |-|Hessian/Burla
p: 'com.xxxx.malm.api.protoss.dto.WithdrawResultDto' is an unknown class in [email protected]:
java.lang.ClassNotFoundException: com.mljr.malm.api.protoss.dto.WithdrawResultDto  |-|
[DubboServerHandler-10.8.104.106:24475-thread-199]  |-|INFO  |-|2018-12-19T16:42:37.940+08:00  |-|c.m.a.b.a.d.s.i.DeductionServiceImpl[268]  |-|【撤销结
算单】查询数据返回信息:{"code":"0","data":{"payUsStatus":2,"no":"18121710054227356449","withholdingRequire":1,"withdrawType":0,"lendingWay":1
,"withdrawStatus":3},"message":"成功","success":true};业务编号为18121710054227356449  |-|
[DubboServerHandler-10.8.104.106:24475-thread-199]  |-|ERROR  |-|2018-12-19T16:42:37.940+08:00  |-|c.m.a.b.a.d.f.BgisDeductionFacadeImpl[96]  |-|【结算
单撤销】系统异常,异常原因:  |-|
java.lang.ClassCastException: java.util.HashMap cannot be cast to com.xxxx.malm.api.mac.dto.WithdrawResultDto
        at com.xxxx.acs.xxxx.apps.deduction.service.impl.DeductionServiceImpl.isCancelByTerm(DeductionServiceImpl.java:273)
        at com.xxxx.acs.xxxx.apps.deduction.service.impl.DeductionServiceImpl.cancelDeduction(DeductionServiceImpl.java:245)
        at com.xxxx.acs.xxxx.apps.deduction.biz.impl.DeductionBizImpl.cancelDeductionBill(DeductionBizImpl.java:110)
        at com.xxxx.acs.xxxx.apps.deduction.facade.BgisDeductionFacadeImpl.cancelDeductionBill(BgisDeductionFacadeImpl.java:88)
        at com.alibaba.dubbo.common.bytecode.Wrapper107.invokeMethod(Wrapper107.java)
        at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:46)
        at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:72)
        at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:53)
        at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:64)
        at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:69)
        at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)
        at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:69)
        at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)
        at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:69)
        at com.alibaba.dubbo.rpc.filter.AccessLogFilter.invoke(AccessLogFilter.java:154)
        at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:69)

前面的warning 信息指出:‘com.xxxx.malm.api.protoss.dto.WithdrawResultDto’ is an unknown class…
后面报 ClassCastException 的却是com.xxxx.malm.api.mac.dto.WithdrawResultDto。

看到这里,我忽然就想到了,应该是其他组的api 中涉及的dto 修改了路径,但是我们用的还是低版本的老路径,在执行下面的代码时,相当于类型的转换(从T到WithdrawResultDto):

WithdrawResultDto withdraw = withdrawResult.getData();

因为转换的时候涉及到读取对象的全路径,全路径不一致,就报错了。

最后就是我们升级下 调用的 api 的版本,使用新路径就好了????