webservice给soapenv:Envelope 添加自定义的命名空间
程序员文章站
2022-04-02 16:15:56
...
soap:Head 自定义命名空间:xw ,使用handler处理消息头
<soap:Head>
<xw:arg0 ></xw:arg0>
</soap:Head>
1.配置Handler
<jaxws:endpoint id="queryUserAddr" address="/queryUserAddr" implementorClass="cn.interfaces.IQueryUser">
<jaxws:implementor>
<bean id="queryUserImpl" class="cn.interfaces.impl.QueryUserImpl">
</bean>
</jaxws:implementor>
<jaxws:handlers>
<bean class="cn.interfaces.impl.PreHeaderHandler"></bean>
</jaxws:handlers>
</jaxws:endpoint>
2.Handler
//<soapenv:Header>里面的元素添加命名空间
public class PreHeaderHandler implements SOAPHandler<SOAPMessageContext> {
public PreHeaderHandler(){
System.out.println("用户验证成功........................");
}
public boolean handleMessage(SOAPMessageContext context) {
Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
QName usernameQName = new QName("http://cc.haoren.com/services", "UsernameToken","xw");
if (isRequest) {
try {
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
soapEnv.addNamespaceDeclaration("web", "http://cc.haoren.com/services");
//if no header, add one
if (soapHeader == null) {
soapHeader = soapEnv.addHeader();
}
String username = "xxx";
String password = "xxxx";
//添加UsernameToken
SOAPHeaderElement usernameHeaderElement = soapHeader.addHeaderElement(usernameQName);
usernameHeaderElement.addTextNode(username);
soapMsg.saveChanges();
} catch (SOAPException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
public Set<QName> getHeaders() {
return null;
}
public boolean handleFault(SOAPMessageContext context) {
return false;
}
public void close(MessageContext context) {}
}
NodeList list=soapBody.getChildNodes();
int count=list.getLength();
Node node=null;
for(int i=0;i<count;i++) {
node=list.item(i);
node.setPrefix("xx");
}
<soap:Head>
<xw:arg0 ></xw:arg0>
</soap:Head>
1.配置Handler
<jaxws:endpoint id="queryUserAddr" address="/queryUserAddr" implementorClass="cn.interfaces.IQueryUser">
<jaxws:implementor>
<bean id="queryUserImpl" class="cn.interfaces.impl.QueryUserImpl">
</bean>
</jaxws:implementor>
<jaxws:handlers>
<bean class="cn.interfaces.impl.PreHeaderHandler"></bean>
</jaxws:handlers>
</jaxws:endpoint>
2.Handler
//<soapenv:Header>里面的元素添加命名空间
public class PreHeaderHandler implements SOAPHandler<SOAPMessageContext> {
public PreHeaderHandler(){
System.out.println("用户验证成功........................");
}
public boolean handleMessage(SOAPMessageContext context) {
Boolean isRequest = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
QName usernameQName = new QName("http://cc.haoren.com/services", "UsernameToken","xw");
if (isRequest) {
try {
SOAPMessage soapMsg = context.getMessage();
SOAPEnvelope soapEnv = soapMsg.getSOAPPart().getEnvelope();
SOAPHeader soapHeader = soapEnv.getHeader();
soapEnv.addNamespaceDeclaration("web", "http://cc.haoren.com/services");
//if no header, add one
if (soapHeader == null) {
soapHeader = soapEnv.addHeader();
}
String username = "xxx";
String password = "xxxx";
//添加UsernameToken
SOAPHeaderElement usernameHeaderElement = soapHeader.addHeaderElement(usernameQName);
usernameHeaderElement.addTextNode(username);
soapMsg.saveChanges();
} catch (SOAPException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
return true;
}
public Set<QName> getHeaders() {
return null;
}
public boolean handleFault(SOAPMessageContext context) {
return false;
}
public void close(MessageContext context) {}
}
NodeList list=soapBody.getChildNodes();
int count=list.getLength();
Node node=null;
for(int i=0;i<count;i++) {
node=list.item(i);
node.setPrefix("xx");
}