我是Java新手,请按照本指南https://spring.io/guides/gs/consuming-web-service/从Java Api调用Soap(.net asmx)服务。当我尝试向Soap Request添加标头时,它返回Java Null Pointer异常。如果我不提供HeaderComposedCallback,则可以调用该服务。
致电网络服务:
ValidateResponse response = (ValidateResponse) getWebServiceTemplate().marshalSendAndReceive(
"http://somewhere.com/service.asmx",
request, new HeaderComposingCallback());
实现WebServiceMessageCallback:
public class HeaderComposingCallback implements WebServiceMessageCallback {
private PrincipalIdentity prinIdentity;
private ProductIdentity prodIdentity;
public HeaderComposingCallback() {
}
@Override
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
((SoapMessage) message).setSoapAction("Validate");
SoapHeader soapHeader = ((SoapMessage) message).getSoapHeader();
try {
prinIdentity.setRequestorId("value");
..
prodIdentity.setAppId("value");
..
JAXBContext context = JAXBContext.newInstance(PrincipalIdentity.class, ProductIdentityHeader.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(prinIdentity, soapHeader.getResult());
marshaller.marshal(prodIdentity, soapHeader.getResult());
} catch (JAXBException e) {
throw new IOException("error while marshalling.");
}
}
}