日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/5/14 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cxf拦截器,实现对接收到的报文和发送出去的报文格式自定义 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

看此篇文章之前可以看看我的這篇文章,有助于更好的理解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");//替換成你需要的格式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拦截器,实现对接收到的报文和发送出去的报文格式自定义的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。