问题

创建了一个JAX-WS处理程序,以将mac地址自动注入到客户端SOAP请求标头中:

文件:MacAddressInjectHandler.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 ”属性吗? 两者之间的“ 空格 ”导致“地址”成为“ mac”元素的属性。

要解决此问题,只需删除以下空格:

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

翻译自: https://mkyong.com/webservices/jax-ws/javax-xml-stream-xmlstreamexception-parseerror-at-rowcolxxx/

Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐