cxf拦截器,实现对接收到的报文和发送出去的报文格式自定义
2017-12-03 16:16
1001 查看
看此篇文章之前可以看看我的这篇文章,有助于更好的理解webservice和这篇文章
http://blog.csdn.net/zhaofuqiangmycomm/article/details/78701566
1,配置文件
?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- web服务实现声明 -->
<bean id="ws63" class="com.thinkgem.jeesite.webservice.service.CustomerWebServiceImpl"></bean>
<!-- web服务配置 :服务地址+服务实现 -->
<jaxws:server address="/customerService">
<jaxws:serviceBean>
<ref bean="ws63"/>
</jaxws:serviceBean>
<!-- 接收报文拦截器,可以自定义接收到报文的格式--->
<jaxws:inInterceptors>
<bean class="com.thinkgem.jeesite.webservice.utils.CAbstractPhaseInterceptor">
<constructor-arg>
<value>receive</value>
</constructor-arg>
</bean>
</jaxws:inInterceptors>
<!-- 发送报文拦截器,可以自定义发送出去报文的格式-->
<jaxws:outInterceptors>
<bean class="com.thinkgem.jeesite.webservice.utils.ArtifactOutInterceptor"/>
</jaxws:outInterceptors>
</jaxws:server>
</beans>
2.发送报文拦截器
2.接收报文拦截器
在这里吐槽一下soap协议的xml,真没restful风格的json好用
http://blog.csdn.net/zhaofuqiangmycomm/article/details/78701566
1,配置文件
?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- web服务实现声明 -->
<bean id="ws63" class="com.thinkgem.jeesite.webservice.service.CustomerWebServiceImpl"></bean>
<!-- web服务配置 :服务地址+服务实现 -->
<jaxws:server address="/customerService">
<jaxws:serviceBean>
<ref bean="ws63"/>
</jaxws:serviceBean>
<!-- 接收报文拦截器,可以自定义接收到报文的格式--->
<jaxws:inInterceptors>
<bean class="com.thinkgem.jeesite.webservice.utils.CAbstractPhaseInterceptor">
<constructor-arg>
<value>receive</value>
</constructor-arg>
</bean>
</jaxws:inInterceptors>
<!-- 发送报文拦截器,可以自定义发送出去报文的格式-->
<jaxws:outInterceptors>
<bean class="com.thinkgem.jeesite.webservice.utils.ArtifactOutInterceptor"/>
</jaxws:outInterceptors>
</jaxws:server>
</beans>
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") .replace("xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"", "xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"") .replace("xmlns:ns2=\"http://service.webservice.jeesite.thinkgem.com/\"","xmlns:ns2=\"http://service.webservice.jeesite.thinkgem.com/\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"") //.replace("xmlns:ns2=\"http://localhost:8080/mycrm/webservice/customerService\"","xmlns:ns1=\"http://tzql.webservice.com/\" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"") .replace("<return>", "<receiveReturn>") .replace("</return>", "</receiveReturn>") .replace("soap:", "soapenv:") .replace("<receiveReturn>", "<receiveReturn xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xsi:type=\"soapenc:string\">") //.replace("soapenvenv", "soapenv") .replace("ns2", "ns1"); 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) */ @Override public 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\"", " ") //.replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "xmlns:cus=\"http://10.80.48.162:8000/mycrm/webservice/customerService\"") .replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "xmlns:ser=\"http://service.webservice.jeesite.thinkgem.com/\"") //.replace("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "xmlns:cus=\"http://localhost:8080/mycrm/webservice/customerService\"") .replace(" xmlns:ns1=\"http://tzgl.webservice.com/\"","") .replace(" xmlns:ns1=\"http://service.webservice.jeesite.thinkgem.com/\"", "") .replace(" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"", "") .replace(" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\"", "") .replace(" xsi:type=\"soapenc:string\"", "") .replace("<receive>", "<ser:receive>") .replace("</receive>", "</ser:receive>"); // 替换后的报文 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好用
相关文章推荐
- 通过C#实现发送自定义的html格式邮件
- Netty实现服务端客户端长连接通讯、心跳检测及自定义报文发送(一)
- 用Python实现在Linux环境发送带附件的邮件,支持文本/html格式
- 利用消息机制实现VC与Delphi之间的通讯(发送自定义消息)
- HTTP POST请求报文格式分析与Java实现文件上传
- 自定义格式字符串随笔(IFormattable,IFormatProvider,ICustomFormatter三接口的实现)
- 翻翻git之---自定义邮件发送按钮SendButton(流程分析,实现思路可以学习下)
- 通过C#发送自定义的html格式邮件
- 基于JSON格式报文的AJAX通用实现
- mapreduce实现writable接口自定义输出格式
- java发送邮件带附件、格式、可以实现转发、秘密转发等操作
- 自定义UITabBar--实现类似新浪微博中间的发送按钮
- vc自定义消息的发送与接收的方法实现
- C#实现 CAT021报文格式解析
- MFC使用SendMessage()发送自定义消息实现进程间通信
- 通过ajax异步向后端发送请求,响应请求向前端传送json格式数据的实现思路
- HTTP POST请求报文格式分析与Java实现文件上传
- HTTP POST请求报文格式分析与Java实现文件上传
- python实现xml转自定义格式文件
- 通过调用qt的assistant来实现自定义文档(qhc格式)