代码网站怎么制作,品质好可以说成品质什么,自己建设网站需要具备哪些条件,门户网站建设平台描述
XFIRE升级CXF框架#xff0c;但是对接的系统不做调整#xff0c;这时候就要保证参数报文和响应报文和以前是一致的。但是不同的框架有不同的规则#xff0c;想要将报文调整的一致#xff0c;就需要用到拦截器拦截报文#xff0c;自定义解析处理。 CXF框架本身就是支…描述
XFIRE升级CXF框架但是对接的系统不做调整这时候就要保证参数报文和响应报文和以前是一致的。但是不同的框架有不同的规则想要将报文调整的一致就需要用到拦截器拦截报文自定义解析处理。 CXF框架本身就是支持配置自定义拦截器的以下是案例。
响应拦截器案例
package com.xinyuan.model.thirdparty.cxf.interceptor;
import org.apache.cxf.interceptor.AttachmentOutInterceptor;
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.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.OutputStream;
import java.io.StringReader;
import java.io.StringWriter;public class NamespacePrefixInterceptor extends AbstractPhaseInterceptorMessage {public NamespacePrefixInterceptor() {// 在发送消息之前的阶段进行拦截super(Phase.PRE_STREAM);// 添加依赖的拦截器addAfter(AttachmentOutInterceptor.class.getName());}Overridepublic void handleMessage(Message message) {try {// 获取输出流OutputStream outputStream message.getContent(OutputStream.class);if (outputStream null) {return;}// 使用 CachedOutputStream 缓存输出流以便我们可以读取和修改它CachedOutputStream cachedOutputStream new CachedOutputStream();message.setContent(OutputStream.class, cachedOutputStream);// 在拦截器处理完毕后将缓存的输出流写回到原始输出流中message.getInterceptorChain().doIntercept(message);// 读取缓存的输出流内容try (CachedOutputStream cos cachedOutputStream) {String response new String(cos.getBytes());// 在这里对 response 进行处理String modifiedResponse processResponse(response);// 将处理后的内容写回到输出流中try (OutputStream finalOutputStream outputStream) {finalOutputStream.write(modifiedResponse.getBytes());}}} catch (Exception e) {e.printStackTrace();}}private String processResponse(String response) {//在这里写处理报文的逻辑return response;}}
在发布接口的代码处添加拦截器
通过dom的方式解析xml并为满足条件的元素添加属性 public static void main(String[] args){String xml soap:Envelope xmlns:soap\http://schemas.xmlsoap.org/soap/envelope/\\n soap:Body\n ns1:queryGysxxResponse xmlns:ns1\http://iface.xt.service.zzxy.com\ xmlns\http://entity.service.zzxy.com\\n ns1:out\n cz111/cz\n dh xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n dzyx xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n frdbmc xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n gsdz xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n gysbhgysbh/gysbh\n gysmc xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n khhmc xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n khhzh xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n lxrmc xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n sj xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n xxdz xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n yzbh xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n zzjgdmh xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n /ns1:out\n /ns1:queryGysxxResponse\n /soap:Body\n /soap:Envelope;try {// 创建DocumentBuilderFactory对象DocumentBuilderFactory factory DocumentBuilderFactory.newInstance();// 设置命名空间感知factory.setNamespaceAware(true);// 创建DocumentBuilder对象DocumentBuilder builder factory.newDocumentBuilder();// 解析XML字符串Document document builder.parse(new InputSource(new StringReader(xml)));// 获取ns1:out元素NodeList outNodes document.getElementsByTagNameNS(http://iface.xt.service.zzxy.com, out);if (outNodes.getLength() 0) {Element outElement (Element) outNodes.item(0);// 获取ns1:out元素下的所有子节点NodeList childNodes outElement.getChildNodes();for (int i 0; i childNodes.getLength(); i) {Node childNode childNodes.item(i);if (childNode.getNodeType() Node.ELEMENT_NODE) {// 修改子节点的命名空间//((Element) childNode).setNamespaceURI(http://entity.service.zzxy.com);((Element) childNode).setAttribute(a,http://entity.service.zzxy.com);}}}// 输出修改后的XMLTransformerFactory transformerFactory TransformerFactory.newInstance();Transformer transformer transformerFactory.newTransformer();DOMSource source new DOMSource(document);StreamResult result new StreamResult(System.out);transformer.transform(source, result);} catch (Exception e) {e.printStackTrace();}}dom4j的方式解析xml对满足条件的元素添加命名空间
public static void main(String[] args) throws DocumentException {String xml soap:Envelope xmlns:soap\http://schemas.xmlsoap.org/soap/envelope/\\n soap:Body\n ns1:queryGysxxResponse xmlns:ns1\http://iface.xt.service.zzxy.com\ xmlns\http://entity.service.zzxy.com\\n ns1:out\n cz111/cz\n dh xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n dzyx xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n frdbmc xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n gsdz xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n gysbhgysbh/gysbh\n gysmc xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n khhmc xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n khhzh xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n lxrmc xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n sj xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n xxdz xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n yzbh xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n zzjgdmh xsi:nil\true\ xmlns:xsi\http://www.w3.org/2001/XMLSchema-instance\/\n /ns1:out\n /ns1:queryGysxxResponse\n /soap:Body\n /soap:Envelope;try {SAXReader reader new SAXReader();Document document reader.read(new StringReader(xml));Element root document.getRootElement();//printElementNames(root);//查找 ns1:out 元素Element ns1OutElement root.element(Body).element(queryGysxxResponse).element(out);// 打印所有的元素名称printElementNames(ns1OutElement);setNamespaceForChildElements(ns1OutElement);} catch (DocumentException e) {e.printStackTrace();}}private static void setNamespaceForChildElements(Element element) {// 创建目标命名空间Namespace targetNamespace new Namespace(, http://entity.service.zzxy.com);// 遍历当前元素的所有子元素for (Object child : element.elements()) {if (child instanceof Element) {Element childElement (Element) child;// 将子元素的 QName 更新为目标命名空间QName newQName new QName(childElement.getName(), targetNamespace);childElement.setQName(newQName);}}}private static void printElementNames(Element element) {// 打印当前元素的名称System.out.println(element.getName());// 递归打印子元素的名称for (Object child : element.elements()) {if (child instanceof Element) {printElementNames((Element) child);}}
}