2016-07-15 187 views
1

是否有可能以簡單的方式在cdata標籤內發送字符串以避開轉義字符? 我對此String使用@XmlCDATA標記,並將其值更改爲當我使用Marshaller時。但是當我想發送請求到SoapUI時,不會添加這個。當我手動添加此標籤(例如在setter中)時,它會轉義字符。Java Web服務客戶端cdata標籤

例如: 如果我使用編組我得到:

<?xml version="1.0" encoding="UTF-8"?> 
<getRequest xmlns="pl/nosd/Get"> 
    <ClientNumber> 
     <![CDATA[<CLIENT_NUMBER>]]> 
    </ClientNumber> 
</getRequest> 

這就是正確的。

但是,當我想給這個使用服務SOAPUI:

sync_Customer_Service = new Sync_Customer_Service(); 
Customer_PortType customer_PortType = sync_Customer_Service.getCustomer_HTTPSPort(); 
GetRequest getRequest = new GetRequest(); 
getRequest.setClientNumber("<CLIENT_NUMBER>"); 
customer_PortType.get(getRequest); 

我得到:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"> 
    <S:Body> 
     <ns2:GetRequestMessage xmlns="pl/nosd/Get" xmlns:ns2="http://pl/nosd/Get/xsd"> 
     <ClientNumber>&lt;CLIENT_NUMBER></ClientNumber> 
     </ns2:GetRequestMessage> 
    </S:Body> 
</S:Envelope> 

我加jaxb.properties與文件:javax.xml.bind.context.factory的= org.eclipse.persistence.jaxb.JAXBContextFactory

和我加入@XMLCdata批註的GetRequest類:

public class GetRequest { 
    @XmlCDATA 
    @XmlElement(name = "ClientNumber", required = true) 
    protected String clientNumber; 
    public String getClientNumber() { 
     return clientNumber; 
    } 
    public void setClientNumber(String value) { 
     this.clientNumber = value; 
    } 

服務和portType類是由jdeveloper通過使用Web服務代理選項生成的。

+1

什麼是有CDATA,而不是一個點逃脫的字符串?這些是相同數據的兩種表示。 – vanje

+0

系統,我發送請求預計該表單。 – krzysiek

回答

0

我找到了解決方案。解決方法不是美觀,但它的工作原理。在客戶端I類添加處理程序:

BindingProvider bp = (BindingProvider)account_PortType; 
    List<Handler> handlerList = bp.getBinding().getHandlerChain(); 
    RequestResponseHandler reqResHelper = null; 
    handlerList.add(new HeaderHandler()); 
    bp.getBinding().setHandlerChain(handlerList); 

而且在處理我與C數據值替換值:

public class HeaderHandler implements SOAPHandler<SOAPMessageContext> { 
      private static final String SYSTEM_ID = "pl/Customer.wsdl"; 
      private static final String NODE_NAME = "ClientNumber"; 
      public boolean handleMessage(SOAPMessageContext context) { 
    try{ 
     Boolean outboundProperty = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 
     if (outboundProperty.booleanValue()) { 
      Object o = context.get(LogicalMessageContext.WSDL_DESCRIPTION); 
      org.xml.sax.InputSource is = (org.xml.sax.InputSource)o; 
      if(is.getSystemId().contains(SYSTEM_ID)){ 
       SOAPMessage message = context.getMessage(); 
       SOAPBody body = message.getSOAPBody(); 
       Node e =getNode(body.getChildNodes()); 
       CDATASection cdataSection = message.getSOAPPart().createCDATASection(e.getFirstChild().getNodeValue()); 
       e.replaceChild(cdataSection, e.getFirstChild()); 
      } 
     } 
    }catch(Exception e){ 
     //TODO logować 
    } 

    return true; 

} 

public Node getNode(NodeList a){ 
    if(a!=null&&a.getLength()>0){ 
     for(int i = 0;i<a.getLength();i++){ 
      Node b = a.item(i); 
      if(b.getNodeName()!=null && b.getNodeName().contains(NODE_NAME)){ 
       return b; 
      } 
      Node node = getNode(b.getChildNodes()); 
      if(node!=null){ 
       return node; 
      } 
     } 
    } 

    return null; 
} 

在soapUI的,我們看到:

<ns6:ClientNumber> 
    <![CDATA[data]]> 
</ns6:ClientNumber>