Java Web服务客户端cdata标记

时间:2016-07-15 21:10:55

标签: java xml jaxb jax-ws jdeveloper

是否有可能以简单的方式在cdata标签内发送字符串以逃避字符? 我为此String使用了@XmlCDATA标记,它将其值更改为使用Marshaller时的值。但是当我想向SoapUI发送请求时,它并没有添加它。当我手动添加此标记时(例如在setter中),它会转义字符。

例如: 如果我使用marshaller,我会得到:

<?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 个答案:

答案 0 :(得分:0)

我找到了解决方案。解决方法不是美,但它有效。在客户端类中,我添加了处理程序:

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

在处理程序中,我用Cdata值替换值:

    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>