2016-08-25 1 views
1

를 사용하여 요청의 문자열 매개 변수에 CDATA 추가는 모든 작업의하지만 생성 된 요청은, 그는 XML이 들어 나는 문자열 매개 변수 (MGRequest)가 CXF</p> <p>요청에 ganerated JAX-WS 클라이언트가있는 경우에만 JAX-WS

<S:Body> 
    <ns5:MGRequest><![CDATA[<mytag>hello</mytag>]]></ns5:MGRequest> 
</S:Body> 

(I 서버를 제어 할 수 있기 때문에 ...)

: I과 같은 신체를 생성해야

<S:Body> 
    <ns5:MGRequest>&lt;mytag&gt;hello&lt;/mytag&gt;</ns5:MGRequest> 
</S:Body> 

: 이런

클라이언트는 표준 JAX-WS 같다 :

@WebService(name = "ServiceSoap") 
@XmlSeeAlso({ ObjectFactory.class}) 
@SOAPBinding(parameterStyle = SOAPBinding.ParameterStyle.BARE) 
public interface ServiceSoap { 

    @WebMethod(operationName = "ProcessMessage") 
    @WebResult(name = "MGResponse") 
    public String processMessage(
     @WebParam(partName = "input", name = "MGRequest") String input); 
} 

그리고 난 같은 전화 :

Service client = new Service(url); 
client.setHandlerResolver(HandlerFactory.build(new LoggerHandler())); 

ServiceSoap service = client.getServiceSoap(); 

String msgToSend = JaxbUtil.jaxbObjToString(xmlObj, false); 
String response = service.processMessage(msgToSend); 

내가 @WebParam 전에 @XmlJavaTypeAdapter(CDataAdapter.class)를 추가 시도했지만 결과가 있었다 :

<S:Body> 
    <ns5:MGRequest>&lt;![CDATA[&lt;mytag&gt;hello&lt;/mytag&gt;]]&gt;</ns5:MGRequest> 
</S:Body> 

여기서 CDataAdapter :

public class CDataAdapter extends XmlAdapter<String, String> { 

    @Override 
    public String marshal(String v) throws Exception { 
     return "<![CDATA[" + v + "]]>"; 
    } 

    @Override 
    public String unmarshal(String v) throws Exception { 
     return v; 
    } 
} 

아카이브하는 방법을 알려주세요. 감사

작업 밤 후

답변

2

나는 해결책을 발견했습니다 이 같은 클라이언트에 javax.xml.ws.handler.Handler를 추가 :

public static HandlerResolver build(final Handler... handlers) { 

    return new HandlerResolver() { 
     @Override 
     public List<Handler> getHandlerChain(PortInfo portInfo) { 
     List<Handler> handlerChain = new ArrayList<Handler>(); 

     if (handlers != null) { 
      for (Handler handler : handlers) { 
      handlerChain.add(handler); 
      } 
     } 
     return handlerChain; 
     } 
    }; 
    } 

: 내 HandlerFactory이 핸들러를 구축

client.setHandlerResolver(HandlerFactory.build(new LoggerHandler(), new CDataHandler())); 

import javax.xml.namespace.QName; 
import javax.xml.soap.Node; 
import javax.xml.soap.SOAPMessage; 
import javax.xml.ws.handler.MessageContext; 
import javax.xml.ws.handler.soap.SOAPHandler; 
import javax.xml.ws.handler.soap.SOAPMessageContext; 

public class CDataHandler implements SOAPHandler<SOAPMessageContext> { 

    @Override 
    public void close(MessageContext context) { 
    } 

    @Override 
    public boolean handleMessage(SOAPMessageContext soapMessage) { 
    try { 
     SOAPMessage message = soapMessage.getMessage(); 
     boolean isOutboundMessage = (Boolean) soapMessage 
      .get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 

     // is a request? 
     if (isOutboundMessage) { 
     // build a CDATA NODE with the text in the root tag 
     Node cddata = (Node) message.getSOAPPart().createCDATASection(
      message.getSOAPBody().getFirstChild().getTextContent()); 

     // add the CDATA's node at soap message 
     message.getSOAPBody().getFirstChild().appendChild(cddata); 

     // remove the text tag with the raw text that will be escaped 
     message.getSOAPBody().getFirstChild() 
      .removeChild(message.getSOAPBody().getFirstChild().getFirstChild()); 

     } 

    } catch (Exception ex) { 
     // fail 
    } 
    return true; 
    } 

    @Override 
    public boolean handleFault(SOAPMessageContext soapMessage) { 
    return true; 
    } 

    @Override 
    public Set<QName> getHeaders() { 
    return Collections.EMPTY_SET; 
    } 
} 

이것은 간단한 클래스이며, 태그가 하나뿐입니다. 텍스트이지만 더 복잡한 시나리오에서는 DOM을 탐색하는 데 필요한 조치를 취할 수 있습니다.

+1

위의 해결 방법을 사용하려면 "message.saveChanges();"를 추가해야합니다. 메시지 메시지 기능을 handleMessage. https://www.mkyong.com/webservices/jax-ws/jax-ws-soap-handler-in-client-side/를 참조하십시오. –

관련 문제