EndPoint类中的Spring WS SOAP响应标头修改

时间:2018-09-13 05:10:11

标签: java soap spring-ws endpoint

我是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;
    }
}

1 个答案:

答案 0 :(得分:0)

我自己解决了。

您可以在部分中看到以下代码:

/ ************************************************** *******************

*创建响应正文和标头

*然后发回

*************************************************** ****************** /

 @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;
    }
}