I am new to Spring WS and in the midst of implementing a service. For this, I am using EndPoint class and can successfully receive a request and send response.
The SOAP Request message contains vital information in SOAP header which I successfully extract. Some information must be sent in SOAP response header also but I am unable to edit or modify the SOAP response header.
I will need help in understanding on what is the best way and best practice to create SOAP Response header together with SOAP Response message.
You can find my EndPoint class below:
@Endpoint
public class EndpointAccountInformationInquiry {
private static final String TARGET_NAMESPACE = "http://www.sample.com/inquiry/GetAccountInformation";
@Autowired
private ServiceAccountInformation service;
@PayloadRoot(localPart = "GetAccountInformationRq", namespace = TARGET_NAMESPACE)
public @ResponsePayload GetAccountInformationRs handleRequest(@RequestPayload GetAccountInformationRq request, MessageContext messageContext) throws JAXBException, TransformerException {
/*****************************************************************
* Parse the request header and body
* Also create response body and header
*****************************************************************/
SaajSoapMessage soapRequest = (SaajSoapMessage) messageContext.getRequest();
SoapHeader soapRequestHeader = soapRequest.getSoapHeader();
SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
SoapHeader soapResponseHeader = soapResponse.getSoapHeader();
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
MyHeaderClassRq myHeaderClassRq = null;
JAXBContext jaxbContext = JAXBContext.newInstance(MyHeaderClassRq.class);
Iterator<SoapHeaderElement> itr = soapRequestHeader.examineAllHeaderElements();
while (itr.hasNext()) {
SoapHeaderElement ele = itr.next();
myHeaderClassRq = (MyHeaderClassRq)jaxbContext.createUnmarshaller().unmarshal(ele.getSource());
transformer.transform(ele.getSource(), soapResponseHeader.getResult());
}
/*****************************************************************
* Call the handler function
* This handler function is asynchronous
*****************************************************************/
service.handleRequest(request, msgHdrRq);
/*****************************************************************
* Set the response body and header over here
*****************************************************************/
//TODO: I want to modify my response header over here....
GetAccountInformationRs response = new GetAccountInformationRs();
return response;
}
}