2017-09-20 8 views
0

저는 속도 템플릿을 사용하여 SOAP 요청을 만듭니다. jax-ws 프레임 워크를 사용하여 웹 서비스 클라이언트를 구현합니다. 아웃 바운드 메시지를 가로 채기 위해 SOAP 처리기를 연결했습니다.SOAPHandler의 SOAP 요청에서 body 요소를 재배치 할 때 오류가 발생했습니다.

계산 된 새 본문으로 본문 내용을 바꾸려고합니다.

내 처리기에서 다음 코드를 사용

public boolean handleMessage(SOAPMessageContext context) { 

    boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY); 
    try { 
     if (outbound) { 
      SOAPMessage msg = context.getMessage(); 
      SOAPPart sp = msg.getSOAPPart(); 
      SOAPEnvelope env = sp.getEnvelope(); 
      SOAPBody body = env.getBody(); 
      body.normalize(); 
      System.out.println(body.getValue()); 
      NodeList list = body.getElementsByTagName("template"); 
      if(list.getLength() > 0) { 
       Element template = (Element) list.item(0); 
       if (template != null) { 
        String newBody = StringEscapeUtils.unescapeHtml(template.getTextContent()); 
        Document bodyElement = XmlUtils.getDocumentFromText(newBody); 
        body.removeContents(); 
        body.addDocument(bodyElement); 

내가 그것을 실행할 때, 나는 다음과 같은 오류 얻을 : 내가 XML을 텍스트에서 본문 내용을 변경할 수있는 방법

org.w3c.dom.DOMException: NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces. 

을 너무 많은 번거 로움없이?

감사합니다.

답변

0

좋아요, 더 자세히 살펴보면 해결책을 찾았습니다.

SOAPMessage msg = context.getMessage(); 
      SOAPPart sp = msg.getSOAPPart(); 
      SOAPEnvelope env = sp.getEnvelope(); 
      SOAPBody body = env.getBody(); 
      body.normalize(); 
      System.out.println(body.getValue()); 
      NodeList list = body.getElementsByTagName("template"); 
      if(list.getLength() > 0) { 
       Element template = (Element) list.item(0); 
       if (template != null) { 
        String newBody = StringEscapeUtils.unescapeHtml(template.getTextContent()); 
        Document bodyElement = XmlUtils.getBody(newBody); 
        body.removeContents(); 
        body.addDocument(bodyElement); 

더 중요한 방법의 XmlUtils.getBody :

public static Document getBody(String fromText) { 
    Document result = null; 

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
    factory.setNamespaceAware(true); 
    try { 
     DocumentBuilder builder = factory.newDocumentBuilder(); 
     result = builder.parse(new ByteArrayInputStream(fromText.getBytes())); 
     NodeList list = result.getElementsByTagNameNS("soapenv", "Body"); 
     if(list.getLength() > 0) { 
      Node body = list.item(0); 
      result = builder.newDocument(); 
      result.adoptNode(body); 
     } 
    } catch(Exception e) { 
     e.printStackTrace(); 
    } 
    return result; 
} 

내가 다시 제기 된 예외를 설명하고, 실제로 내 앞의 코드에서 factory.setNamespaceAware(true) 부분이었다 여기에 코드입니다.

문제가 해결되었습니다.