cxf实现的拦截器因为webservice返回报文太长无法拿到数据

zwk815645088 2018-01-05 01:35:19
以下OutInterceptor拦截器代码部分实现,因为webservice返回报文太长,导致数据无法回写到流中,无法拿到返回报文,最终xml = IOUtils.toString(in); 为null,求大大们帮忙解决啊。。。
//获取接口返回信息
OutputStream os = message.getContent(OutputStream.class);
CachedStream cs = new CachedStream();
message.setContent(OutputStream.class, cs);
message.getInterceptorChain().doIntercept(message);
CachedOutputStream csnew = (CachedOutputStream) message
.getContent(OutputStream.class);
InputStream in = csnew.getInputStream();
//获取返回报文
xml = IOUtils.toString(in);
//解析返回报文
Document document = DocumentHelper.parseText(xml);

...全文
865 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
羊村大少 2021-05-14
  • 打赏
  • 举报
回复
这个可行!
qq_33712867 2020-06-09
  • 打赏
  • 举报
回复

@Override
public void handleMessage(Message message) throws Fault {
try {
OutputStream os = message.getContent(OutputStream.class);
ByteArrayOutputStream cs = new ByteArrayOutputStream();
message.setContent(OutputStream.class, cs);
message.getInterceptorChain().doIntercept(message);
ByteArrayOutputStream csnew = (ByteArrayOutputStream) message.getContent(OutputStream.class);
ByteArrayInputStream bis = new ByteArrayInputStream(csnew.toByteArray());
String json = IOUtils.toString(bis);
InterfaceAccessLog interfaceAccessLog =InterfaceAccessLogHolder.getInterfaceAccessLog();
interfaceAccessLog.setResponse(changeJsonstr(json));
interfaceAccessLog.setUpdatetime(new Date());
service.insertInterfaceAccessLog(interfaceAccessLog);
IOUtils.copy(new ByteArrayInputStream(json.getBytes("utf-8")), os);
cs.close();
os.flush();
message.setContent(OutputStream.class, os);
} catch (IOException e) {
e.printStackTrace();
}
}

完美解决
zwk815645088 2018-01-06
  • 打赏
  • 举报
回复 3
目前发现,基于cxf实现的webservice接口返回字符串太长的时候,会造成OutInterceptor拦截器中的SoapMessage的buf流为null,如果想要在拦截器中分析报文,显然会有问题,具体原因未找到,通过分析源码Message.getContent(Class<T> format) 方法来获取信息,可传参数为(XMLStreamReader.class, XMLStreamWriter.class, InputStream.class, OutputStream.class, List.class, Exception.class, Node.class, DelegatingInputStream.class),由于流中是null,因此message.getContent(OutputStream.class)是行不通的,通过webService返回获取返回值是通过List获取(目前看是这样),因此我的解决办法是message.getContent(List.class);获取List ,通过反射来拿报文: List list = message.getContent(List.class); for(Object o : list){ System.out.println("=========="+o.getClass()); Class oas = o.getClass(); Field[] fs = oas.getDeclaredFields(); for(int i = 0 ; i < fs.length; i++){ Field f = fs[i]; f.setAccessible(true); Object val = f.get(o); }

50,527

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧