Serialized class io.undertow.servlet.spec.HttpServletResponseImpl must implement java.io.Serializabl

目录

1.问题

2.说明及解决方案

3.总结


1.问题

使用dubbo进行服务间的调用时报错如下:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is com.alibaba.dubbo.rpc.RpcException: Failed to invoke the method dowloadTemplate in the service com.kingagroot.info.msapi.kingdatabase.attribute.AttributeCustomizeService. Tried 3 times of the providers [10.1.36.55:20882] (1/1) from the registry 10.120.7.23:2181 on the consumer 10.1.36.55 using the dubbo version 2.6.0. Last error is: Failed to invoke remote method: dowloadTemplate, provider: dubbo://10.1.36.55:20882/com.kingagroot.info.msapi.kingdatabase.attribute.AttributeCustomizeService?actives=0&anyhost=true&application=prismapi&check=false&default.check=false&default.group=kingdatabase_lin&default.timeout=3000&dispatcher=message&dubbo=2.6.0&generic=false&interface=com.kingagroot.info.msapi.kingdatabase.attribute.AttributeCustomizeService&loadbalance=roundrobin&methods=getMapDataList,dowloadTemplate,getInventoryList,getEditCols,delInventory,addInventory,editInventory&owner=lin&pid=30012&register.ip=10.1.36.55&remote.timestamp=1716282273748&side=consumer&timeout=5000&timestamp=1716282274712, cause: Failed to send message Request [id=4, version=2.0.0, twoway=true, event=false, broken=false, data=RpcInvocation [methodName=dowloadTemplate, parameterTypes=[class com.kingagroot.info.domain.params.attribute.InventoryDowloadSpecDomain, interface javax.servlet.http.HttpServletResponse], arguments=[InventoryDowloadSpecDomain(colId=[1036, 1038, 1039], docId=null), io.undertow.servlet.spec.HttpServletResponseImpl@3bbb73f7], attachments={path=com.kingagroot.info.msapi.kingdatabase.attribute.AttributeCustomizeService, interface=com.kingagroot.info.msapi.kingdatabase.attribute.AttributeCustomizeService, version=0.0.0, timeout=5000, group=kingdatabase_lin}]] to /10.1.36.55:20882, cause: Serialized class io.undertow.servlet.spec.HttpServletResponseImpl must implement java.io.Serializable

分析后,进行服务间调用时传递了response作为参数,因为response自身是没有实现序列化接口的,因为是不能被序列化的,所以报错Serialized class io.undertow.servlet.spec.HttpServletResponseImpl must implement java.io.Serializable 

2.说明及解决方案

为什么要再调用其他服务时传递response那,因为这是个文件下载的接口,当时想的是要把response传递到对应服务的service中进行文件下载的操作,因为出现了这个问题。

首先response肯定是不能实现Serializable接口的,因为涉及到文件下载的操作,需要调用对应的service服务获取要下载的数据,然后再controller中直接进行下载操作,不要将response作为参数进行传递,当然如果不是服务间的调用,而是当前服务的controller调用当前服务的service,可以将response作为参数进行传递。但是如果需要进行序列化操作时则无法实现

示例:

    @ApiOperation(value = "下载导入模板")
    @RequestMapping(value = "/mb1001", method = {RequestMethod.POST})
    public void mb20406(@RequestBody InventoryDowloadDomain domain, HttpServletResponse response) throws Exception {
        InventoryDowloadDomain.RequestBody reqBody = domain.getReqBody();
        InventoryDowloadSpecDomain dowDomain = new InventoryDowloadSpecDomain();
        dowDomain.setDocId(reqBody.getDocId());
        dowDomain.setColId(reqBody.getColId());
        String fileName = "导入模板" + DateFormatUtils.format(new Date(), CommonContants.DATE_TYPE) + CommonContants.TYPE_XLSX;
        response.setContentType(CommonContants.CONTENT_TYPE);
        response.setCharacterEncoding(CommonContants.UTF8);
        response.setHeader(Header.CONTENT_DISPOSITION.getValue(), CommonContants.CONTENT_TYPE + URLEncoder.encode(fileName, CommonContants.UTF8));
        DowloadDomain dowloadDomain = attributeCustomizeService.dowloadTemplate(dowDomain);
        List<List<Object>> total = new ArrayList<>();
        EasyExcel.write(response.getOutputStream()).head(dowloadDomain.getHeads()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy())
                .registerWriteHandler(new CustomSheetWriteHandler(dowloadDomain.getValList()))
                .sheet("模板").doWrite(total);

    }

3.总结

在Java中,对象序列化和反序列化操作通常在以下情况下进行:

  1. 网络传输:当需要在网络上传输对象时,可以将对象序列化为字节流,然后在接收端进行反序列化操作。

  2. 对象持久化:将对象保存到磁盘或数据库时,可以先将对象序列化为字节流,然后在需要的时候进行反序列化来重新构建对象。

  3. 进程间通信:在不同进程之间需要共享对象时,可以将对象序列化并通过进程间通信机制传输,然后在接收端进行反序列化还原对象。

序列化和反序列化是Java中用于实现对象持久化和跨系统交互的重要机制。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值