cxf拦截器,实现对接收到的报文和发送出去的报文格式自定义
生活随笔
收集整理的這篇文章主要介紹了
cxf拦截器,实现对接收到的报文和发送出去的报文格式自定义
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
看此篇文章之前可以看看我的這篇文章,有助于更好的理解webservice和這篇文章
http://blog.csdn.net/zhaofuqiangmycomm/article/details/78701566
?
1,配置文件
?
?
?
?
?
2.發送報文攔截器
package com.thinkgem.jeesite.webservice.utils;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);System.out.println("replaceBegin"+xml);xml=xml.replace("return", "receiveReturn");//替換成你需要的格式System.out.println("replaceAfter"+xml);//這里對xml做處理,處理完后同理,寫回流中 IOUtils.copy(new ByteArrayInputStream(xml.getBytes("UTF-8")), 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 { } } }?
?
?
2.接收報文攔截器
?
package com.thinkgem.jeesite.webservice.utils;import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream;import org.apache.cxf.binding.soap.Soap11; import org.apache.cxf.binding.soap.SoapVersion; import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.message.Message; import org.apache.cxf.phase.AbstractPhaseInterceptor;/*** <p>ClassName: CAbstractPhaseInterceptor</p>* <p>Description: cxf攔截器</p>* <p>Author: hff</p>* <p>Date: 2017-4-13</p>*/ public class CAbstractPhaseInterceptor extends AbstractPhaseInterceptor<Message> {/*** <p>Description: TODO</p>* @param phase*/public CAbstractPhaseInterceptor(String phase) {super(phase);}/* (non-Javadoc)* <p>Title: handleMessage</p>* <p>Description: </p>* @param message* @throws Fault* @see org.apache.cxf.interceptor.Interceptor#handleMessage(org.apache.cxf.message.Message)*/@Overridepublic void handleMessage(Message message) throws Fault {InputStream is = message.getContent(InputStream.class);if (is != null) {try {String str = IOUtils.toString(is);// 原請求報文System.out.println("====> request xml=\r\n" + str);// 把siebel格式的報文替換成符合cxf帶前綴的命名空間str = str.replace("ns1:","").replace("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"", " ");// 替換后的報文System.out.println("====> replace xml=\r\n" + str);InputStream ism = new ByteArrayInputStream(str.getBytes("UTF-8"));message.setContent(InputStream.class, ism);} catch (IOException e) {e.printStackTrace();}}}}3,推薦用soapui來調試,調試到你需要的格式
在這里吐槽一下soap協議的xml,真沒restful風格的json好用
?
?
?
?
總結
以上是生活随笔為你收集整理的cxf拦截器,实现对接收到的报文和发送出去的报文格式自定义的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python大顶堆
- 下一篇: 帮帮忙解决一下,谢谢