您的位置:首页 > 其它

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.发送报文拦截器

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好用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息