Java 如何从cxf outInterceptor获取响应体
我正在写一些代码来收集一些控制器的请求参数和响应体 由于项目框架是apache CXF,其版本是Java 如何从cxf outInterceptor获取响应体,java,cxf,Java,Cxf,我正在写一些代码来收集一些控制器的请求参数和响应体 由于项目框架是apache CXF,其版本是3.1.18 我编写了一个拦截器,它扩展了AbstractPhaseInterceptor来收集phasephase.RECEIVE中的参数,该拦截器正在工作 但是当一个write-an-outInterceptor扩展AbstractPhaseInterceptor来收集控制器的响应时,我发现我没有办法这样做,在拦截器中只有一个方法handleMessage(Message Message),我无法
3.1.18
我编写了一个拦截器,它扩展了AbstractPhaseInterceptor来收集phasephase.RECEIVE中的参数,该拦截器正在工作
但是当一个write-an-outInterceptor扩展AbstractPhaseInterceptor来收集控制器的响应时,我发现我没有办法这样做,在拦截器中只有一个方法handleMessage(Message Message)
,我无法从消息中获取任何我想要的东西
有人能帮我吗?我是CXF的新手。谢谢 我从另一个blob中找到了答案
package XXX.web.webservice.interceptor;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.cxf.io.CachedOutputStream;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.log4j.Logger;
public class ArtifactOutInterceptor extends AbstractPhaseInterceptor<Message>{
private static final Logger log = Logger.getLogger(ArtifactOutInterceptor.class);
public ArtifactOutInterceptor() {
//这儿使用pre_stream,意思为在流关闭之前
super(Phase.PRE_STREAM);
}
public void handleMessage(Message message) {
try {
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();
String xml = IOUtils.toString(in);
//这里对xml做处理,处理完后同理,写回流中
IOUtils.copy(new ByteArrayInputStream(xml.getBytes()), os);
cs.close();
os.flush();
message.setContent(OutputStream.class, os);
} catch (Exception e) {
log.error("Error when split original inputStream. CausedBy : " + "\n" + e);
}
}
private class CachedStream extends CachedOutputStream {
public CachedStream() {
super();
}
protected void doFlush() throws IOException {
currentStream.flush();
}
protected void doClose() throws IOException {
}
protected void onWrite() throws IOException {
}
}
}
包XXX.web.webservice.interceptor;
导入java.io.ByteArrayInputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入org.apache.commons.io.IOUtils;
导入org.apache.cxf.io.CachedOutputStream;
导入org.apache.cxf.message.message;
导入org.apache.cxf.phase.AbstractPhaseInterceptor;
导入org.apache.cxf.phase.phase;
导入org.apache.log4j.Logger;
公共类ArtifactOutInterceptor扩展了AbstractPhaseInterceptor{
私有静态最终记录器log=Logger.getLogger(ArtifactOutInterceptor.class);
公共ArtifactOutInterceptor(){
//这儿使用前河意思为在流关闭之前
超级(相位预流);
}
公共无效handleMessage(消息消息){
试试{
OutputStream os=message.getContent(OutputStream.class);
CachedStream cs=新的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);
//这里对xml做处理,处理完后同理,写回流中
copy(新的ByteArrayInputStream(xml.getBytes()),os);
cs.close();
os.flush();
message.setContent(OutputStream.class,os);
}捕获(例外e){
log.error(“拆分原始inputStream时出错。原因:“+”\n“+e”);
}
}
私有类CachedStream扩展CachedOutStream{
公共缓存流(){
超级();
}
受保护的void doFlush()引发IOException{
currentStream.flush();
}
受保护的void doClose()引发IOException{
}
受保护的void onWrite()引发IOException{
}
}
}