我正在java中开发一个肥皂客户端。
肥皂请求
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:com="http://myWs.com/com" xmlns:re="http://myWs.com/myAlert">
<soapenv:Header>
<com:MYHeader>
<com:Auth>
<com:MyID>1312</com:MyID>
<com:Pwd>test213</com:Pwd>
</com:Auth>
</com:MYHeader>
</soapenv:Header>
<soapenv:Body>
<re:MyRequest>
<re:MyOrder>
<re:MyItems>
<re:ItemCode>TEST</re:ItemCode>
<re:ItemName>TEST</re:ItemName>
<re:ItemRef>TEST</re:ItemRef>
</re:MyItems>
</re:MyOrder>
</re:MyRequest>
</soapenv:Body>
</soapenv:Envelope>
我使用Spring Framework的WeberViceTemplate食用肥皂服务。但是当我尝试时,我发现"主体"部分的请求对象完美地进行。但是标题部分为null.my soap客户端:
肥皂:
public Object callWebService(MyRequest request){
MyResponse myObj = (MyResponse)getWebServiceTemplate().marshalSendAndReceive("http://localhost:9000/MyServices/ws/soap/MyService", request,new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
try {
SoapMessage soapMessage = (SoapMessage)message;
SoapHeader header = soapMessage.getSoapHeader();
StringSource headerSource = new StringSource("<MYHeader>n<Auth>n<MyID>1312</MyID>n<Pwd>test213</Pwd>n</Auth>n</MYHeader>");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, header.getResult());
} catch (Exception e) {
System.out.println("ERROR : "+e);
}
}
});
MyResponse response= (MyResponse) myObj;
return myObj;
}
在上面的"请求"中,有肥皂剧的身体部分。
从我的看法中,使用saajsoapmessage更简单,更干净。这是Dowithmessage示例方法代码。让我知道你的想法!
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.xml.transform.StringSource;
public void doWithMessage(WebServiceMessage message) {
if (message instanceof SaajSoapMessage) {
try {
SaajSoapMessage soapMessage = (SaajSoapMessage) message;
SoapHeader soapHeader = soapMessage.getSoapHeader();
StringSource headerSource = new StringSource("<MYHeader>n<Auth>n<MyID>1312</MyID>n<Pwd>test213</Pwd>n</Auth>n</MYHeader>");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(headerSource, soapHeader.getResult());
} catch (TransformerFactoryConfigurationError | TransformerException e) {
logger.error(e.getMessage(), e);
}
}
}
我能够弄清楚这个问题。我能够使用jaxbcontext添加标头,以实现添加标头。我已经修改了我的代码,如链接中所述,像gem.i一起提供以下代码以供参考:
代码
public Object callWebService(MyRequest request){
MyResponse myObj = (MyResponse)getWebServiceTemplate().marshalSendAndReceive("http://localhost:9000/MyServices/ws/soap/MyService", request,new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
try {
SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();
// create the header element
ObjectFactory factory = new ObjectFactory();
MYHeader headerType = factory.createMYHeader();
Auth authType = new Auth();
authType.setMyID("1010");
authType.setPwd("myPwd");
headerType.setAuth(authType);
JAXBElement<MYHeader> headers =
factory.createMYHeader(headerType);
// create a marshaller
JAXBContext context = JAXBContext.newInstance(MYHeader.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(headers, soapHeader.getResult());
} catch (Exception e) {
System.out.println("ERROR : "+e);
}
}
});
MyResponse response= (MyResponse) myObj;
return myObj;
}