如何添加空xmlns =""标记到标题元素?
让我们想象一下,我们在两台服务器上有相同的项目
在这个项目中,我使用了下面的SOAPHandler
第一个服务器发送请求,空xmlns =""标记
<username xmlns="">test</username><password xmlns="">test</password>
,但第二个服务器发送请求没有xmlns =&#34;&#34;标签。
<username>test</username><password>test</password>
我需要xmlns =&#34;&#34;标记在每个服务器中。
我该如何解决这个问题?
import java.util.Collections;
import java.util.Set;
import javax.xml.namespace.QName;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
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;
import org.apache.log4j.Logger;
/**
*
* @author BashirovMA
*/
public class MyHandler implements SOAPHandler<SOAPMessageContext> {
static final Logger log = Logger.getLogger(MyHandler.class);
private String username;
private String password;
public MyHandler() {
this.username = "";
this.password = "";
}
public MyHandler(String username, String password) {
this.username = username;
this.password = password;
}
public MyHandler(String username, String password) {
this.username = username;
this.password = password;
}
@Override
public boolean handleMessage(SOAPMessageContext smc) {
SOAPMessage msg = smc.getMessage();
Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
if (outboundProperty.booleanValue()) {
try {
SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
if (envelope.getHeader() != null) {
envelope.getHeader().detachNode();
}
SOAPHeader header = envelope.addHeader();
SOAPElement root = header.addHeaderElement(envelope.createName("messageHeader", "", "http://services.ws.com/"));
SOAPElement el1 = root.addChildElement(envelope.createName("username", "", ""));
el1.setValue(String.valueOf(username));
SOAPElement el2 = root.addChildElement(envelope.createName("password", "", ""));
el2.setValue(String.valueOf(password));
msg.saveChanges();
} catch (Exception e) {
e.printStackTrace();
log.error(e);
}
} else {
try {
SOAPMessage message = smc.getMessage();
message.writeTo(System.out);
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex);
}
}
return true;
}
@Override
public Set<QName> getHeaders() {
return null;
}
@Override
public boolean handleFault(SOAPMessageContext messageContext) {
return true;
}
@Override
public void close(MessageContext context) {
}
}