cxf拦截器,实现对接收到的报文和发送出去的报文格式自定义

看此篇文章之前可以看看我的这篇文章,有助于更好的理解webservice和这篇文章

http://blog.csdn.net/zhaofuqiangmycomm/article/details/78701566

 

1,配置文件

 

  1. ?xml version="1.0" encoding="utf-8"?>  
  2. <beans   xmlns="http://www.springframework.org/schema/beans"   
  3.         xmlns:jaxws="http://cxf.apache.org/jaxws"  
  4.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.         xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                             http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  
  7.                             http://cxf.apache.org/jaxws  
  8.                             http://cxf.apache.org/schemas/jaxws.xsd">  
  9.       
  10.       
  11.     <bean id="ws63" class="com.thinkgem.jeesite.webservice.service.CustomerWebServiceImpl">bean>  
  12.       
  13.     <jaxws:server address="/customerService">  
  14.         <jaxws:serviceBean>  
  15.             <ref bean="ws63"/>  
  16.         jaxws:serviceBean>  
  17.         
  18.        
  19. <jaxws:inInterceptors> 
  20. <bean class="com.thinkgem.jeesite.webservice.utils.CAbstractPhaseInterceptor">
  21.  <constructor-arg> 
  22. <value>receivevalue>
  23.  constructor-arg> 
  24. bean>
  25. jaxws:inInterceptors> 
  26.  
  27. <jaxws:outInterceptors>  
  28.  <bean class="com.thinkgem.jeesite.webservice.utils.ArtifactOutInterceptor"/> 
  29. jaxws:outInterceptors>
  30.  
  31. jaxws:server>
  32. 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{ 
    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;

/**
 * 

ClassName: CAbstractPhaseInterceptor

*

Description: cxf拦截器

*

Author: hff

*

Date: 2017-4-13

*/ public class CAbstractPhaseInterceptor extends AbstractPhaseInterceptor { /** *

Description: TODO

* @param phase */ public CAbstractPhaseInterceptor(String phase) { super(phase); } /* (non-Javadoc) *

Title: handleMessage

*

Description:

* @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\"", " "); // 替换后的报文 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)