javax.xml.stream.XMLStreamException:[row、col]のParseError:[x、xx]

問題

クライアント側のSOAPリクエストヘッダにmacアドレスを自動的に挿入するJAX-WSハンドラを作成しました。

ファイル:MacAddressInject Handler.java

public class MacAddressInjectHandler implements SOAPHandler<SOAPMessageContext>{

   @Override
   public boolean handleMessage(SOAPMessageContext context) {

   //......
   //get mac address
    String mac = getMACAddress();

   //add a soap header, name as "mac address"
    QName qname = new QName("http://ws.mkyong.com/", "mac address");
    SOAPHeaderElement soapHeaderElement = soapHeader.addHeaderElement(qname);

    soapHeaderElement.setActor(SOAPConstants.URI__SOAP__ACTOR__NEXT);
    soapHeaderElement.addTextNode(mac);
    soapMsg.saveChanges();
       //......

   }
  //......
}

SOAPメッセージが生成され、サービスのプロバイダ(またはサーバー)に送信されると、すぐに次のエラーメッセージが返されます。

com.sun.xml.internal.ws.streaming.XMLStreamReaderException:
   XML reader error: javax.xml.stream.XMLStreamException: ParseError at[row,col]:[1,110]   Message: Attribute name "address" associated with
   an element type "mac" must be followed by the ' = ' character.
  //...
Caused by: javax.xml.stream.XMLStreamException: ParseError at[row,col]:[1,110]   Message: Attribute name "address" associated with an element type
   "mac" must be followed by the ' = ' character.
  //...

解決策

`XMLStreamException`は、無効な形式を含む無効なSOAPメッセージを送信しようとしていることを示しています。上記のSOAPクライアントから、次のような同様のSOAPメッセージを生成することができます。

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Header>
    <mac address xmlns="http://ws.mkyong.com/"
        xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
        SOAP-ENV:actor="http://schemas.xmlsoap.org/soap/actor/next">
            90-4C-E5-44-B9-8F
    </mac address>
  </S:Header>
  <S:Body>
    <ns2:getServerName xmlns:ns2="http://ws.mkyong.com/"/>
  </S:Body>
</S:Envelop

そして、 " mac address "属性に気づくでしょうか?その間にある " space "は "address"を "mac"要素の属性としています。

これを修正するには、次のようなスペースを削除してください:

QName qname = new QName("http://ws.mkyong.com/", "macaddress");