Java 如何使用ApacheCXF获取http请求(soap)的有效负载。使用拦截器时缺少信封闭包
我试图将自定义头附加到cxf消息中,这是可行的,但我对消息负载有一些问题Java 如何使用ApacheCXF获取http请求(soap)的有效负载。使用拦截器时缺少信封闭包,java,spring,apache,cxf,interceptor,Java,Spring,Apache,Cxf,Interceptor,我试图将自定义头附加到cxf消息中,这是可行的,但我对消息负载有一些问题 public HttpHeaderInterceptor() { super(Phase.USER_PROTOCOL); } public void handleMessage(Message message) throws Fault { try { Map<String, List<String>> headers = (HashMap<String,
public HttpHeaderInterceptor() {
super(Phase.USER_PROTOCOL);
}
public void handleMessage(Message message) throws Fault {
try {
Map<String, List<String>> headers = (HashMap<String, List<String>>) message.get(Message.PROTOCOL_HEADERS);
if(validationFlag){
headerInfo = new ArrayList<String>();
String messageContent="empty";
OutputStream os = message.getContent(OutputStream.class);
StringBuilder responsePayload = new StringBuilder();
CachedOutputStream cos = (CachedOutputStream) os;
try {
cos.writeCacheTo(responsePayload);
messageContent = IOUtils.toString(cos.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
//here im getting hashCode of message payload and add to header
byte[] messageByte = messageContent.getBytes("UTF-8");
byte[] doFinal = mac.doFinal(messageByte);
StringBuilder sb = new StringBuilder();
sb.append(" Checksum:");
sb.append(Hex.encodeHex(doFinal));
headerInfo.add(scValidationCode);
headerInfo.add(sb.toString());
headers.put("Warning", headerInfo);
}
} catch ( Exception e) {
throw new Fault(e);
}
}
messageContent是相等的,例如:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:someNamespace xmlns:ns2="http://something.com>some Soap request body</ns2:someNamespace>
一些Soap请求主体
因此,在我的消息内容中,我丢失了
。
我已经尝试更改org.apache.cxf.phase阶段,但我的消息负载仍然没有soap信封关闭
我不知道为什么当我试图获取message.getContent(OutputStream.class)时,我只丢失了soap闭包。
其他一切都很好。HTTP标头正在追加新值 好的,我已经通过了几乎所有的cxf链,找到了解决方案 soap信封和主体闭包被添加到soap SoapOutineInterceptor中,但在子类中,正是从结束链添加到SoapOutEndingInterceptor中 因此,如果您想通过soap闭包获得完整的负载,您必须以两种方式放置自定义拦截器:
super(Phase.PRE_STREAM_ENDING);
addBefore(StaxOutInterceptor.StaxOutEndingInterceptor.class.getName());
或者第二种方式:
super(Phase.WRITE_ENDING);
addAfter(SoapOutInterceptor.SoapOutEndingInterceptor.class.getName());
玩得开心
super(Phase.PRE_STREAM_ENDING);
addBefore(StaxOutInterceptor.StaxOutEndingInterceptor.class.getName());
super(Phase.WRITE_ENDING);
addAfter(SoapOutInterceptor.SoapOutEndingInterceptor.class.getName());