Я новичок в Spring WS и занимаюсь реализацией службы. Для этого я использую класс EndPoint и могу успешно получать запрос и отправлять ответ.
Сообщение запроса SOAP содержит важную информацию в заголовке SOAP, который я успешно извлек. Некоторая информация также должна быть отправлена в заголовке ответа SOAP, но я не могу редактировать или изменять заголовок ответа SOAP.
Мне понадобится помощь в понимании того, как лучше всего создать заголовок ответа SOAP вместе с сообщением ответа SOAP.
Вы можете найти мой класс EndPoint ниже:
@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;
}
}
Я сам решил.
Вы можете увидеть приведенный ниже код в разделе:
/ *********************************************** ****************
* Создание тела и заголовка ответа
* И отправить обратно
************************************************ *************** /
@Endpoint
public class EndpointAccountInformationInquiry {
// private Logger logger = Logger.getLogger(EndpointAccountInformationInquiry.class);
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();
MyHeaderClassRq myHeaderClassRq = null;
JAXBContext jaxbContext = JAXBContext.newInstance(MyHeaderClassRq.class);
Iterator<SoapHeaderElement> itr = reqheader.examineAllHeaderElements();
while (itr.hasNext()) {
SoapHeaderElement ele = itr.next();
myHeaderClassRq = (MyHeaderClassRq)jaxbContext.createUnmarshaller().unmarshal(ele.getSource());
}
/*****************************************************************
* Call the handler function
* This handler function is asynchronous
*****************************************************************/
service.handleRequest(request, myHeaderClassRq);
/*****************************************************************
* Create response body and header
* And send back
*****************************************************************/
//Response header
MyHeaderClassRs myHeaderClassRs = new MsgHdrRs();
//Set header values here
//Response body
GetAccountInformationRs response = new GetAccountInformationRs();
/*****************************************************************
* Send response back
*****************************************************************/
jaxbContext = JAXBContext.newInstance(MyHeaderClassRs.class);
jaxbContext.createMarshaller().marshal(myHeaderClassRs, soapResponseHeader.getResult());
return response;
}
}