Web Services 如何从请求中读取SOAP Header信息并将其添加到Spring Web服务中的响应

lzfw57am  于 2023-08-06  发布在  Spring
关注(0)|答案(4)|浏览(136)

我正在使用Spring Web Services。我需要在请求和响应消息中添加一些自定义元素。应该是这样的:

<soapenv:Envelope>
   <soapenv:Header>
      <tid:SplsTID>
         <tid:Trantype>123</tid:Trantype>
         <tid:Tranver>234</tid:Tranver>
      </tid:SplsTID>
   </soapenv:Header>
   <soapenv:Body>
      <get:GetOrderNumberRequest LoggingLevel="REGULAR" MonitorFlag="Y">
         <get:Header>
            <get:TransactionId>111</get:TransactionId>
            <get:SourceSystemId>SOMS</get:SourceSystemId>
            <get:DateTime>2011-11-11T11:11:11</get:DateTime>
         </get:Header>
         <get:Body>
            <get:StaplesOrderNumber RangeFlag="N" ReleaseFlag="N">
               <get:OrderNumber Count="1" End="11" Start="9"/>
            </get:StaplesOrderNumber>
         </get:Body>
      </get:GetOrderNumberRequest>
   </soapenv:Body>
</soapenv:Envelope>

字符串
通过修改WSDL文件,我能够在request中的<soapenv:Header>下追加<tid:SplsTID>。看起来像这样:

<wsdl:message name="GetOrderNumberRequest">
        <wsdl:part element="tns:GetOrderNumberRequest" name="GetOrderNumberRequest">
        </wsdl:part>
        <wsdl:part element="sch1:SplsTID" name="SplsTID">
        </wsdl:part>
    </wsdl:message>
    <wsdl:message name="GetOrderNumberResponse">
        <wsdl:part element="tns:GetOrderNumberResponse" name="GetOrderNumberResponse">
        </wsdl:part>
        <wsdl:part element="sch1:SplsTID" name="SplsTID">
        </wsdl:part>
    </wsdl:message>
    <wsdl:portType name="ONAS">
        <wsdl:operation name="GetOrderNumber">
            <wsdl:input message="tns:GetOrderNumberRequest" name="GetOrderNumberRequest">
            </wsdl:input>
            <wsdl:output message="tns:GetOrderNumberResponse" name="GetOrderNumberResponse">
            </wsdl:output>
        </wsdl:operation>
    </wsdl:portType>


问题是,我想从请求中读取<tid:SplsTID>部分,并想将其附加到响应的soap头部分,但这没有发生。我正在使用基于注解的端点。什么是代码,将读取SOAP头,并将追加相同的响应。
目前我的终点类是:

@Endpoint
public class OrderNumberServiceEndPoint {
    public static final String NAMESPACE_URI = "http://schemas.staples.com/onas/getOrderNumber";

    /**
     * The local name of the expected request.
     */
    public static final String REQUEST_LOCAL_NAME = "GetOrderNumberRequest";

    /**
     * The local name of the created response.
     */
    public static final String RESPONSE_LOCAL_NAME = "GetOrderNumberResponse";

    private GetOrderNumberService getOrderNumberService;

    public void setGetOrderNumberService(
            GetOrderNumberService getOrderNumberService) {
        this.getOrderNumberService = getOrderNumberService;
    }

    @PayloadRoot(localPart = REQUEST_LOCAL_NAME, namespace = NAMESPACE_URI)
    public GetOrderNumberResponse processOrderNumberRequest(
            GetOrderNumberRequest request) throws Exception {
        GetOrderNumberResponse response = null;
        try{
        response = getOrderNumberService.executeRequest(request);
        }catch(CannotCreateTransactionException e){
            logger.error(ErrorConstants.ERROR_E17);
            throw new ServiceException(ErrorConstants.ERROR_E17);
        }
        return response;
    }

}


如果需要更多细节,请告诉我。如果你能帮忙的话,我将不胜感激。

jdzmm42g

jdzmm42g1#

最后,我成功地从request中阅读了soap头,并将其附加到response中。这就是我的端点方法现在的样子:

@PayloadRoot(localPart = REQUEST_LOCAL_NAME, namespace = NAMESPACE_URI)
    @ResponsePayload
    public GetOrderNumberResponse processOrderNumberRequest(
            @RequestPayload GetOrderNumberRequest request,
            MessageContext messageContext) throws Exception {
        logger.info("Request Received");
        // read SOAP Header from request and append in response
        SaajSoapMessage soapRequest = (SaajSoapMessage) messageContext
                .getRequest();
        SoapHeader reqheader = soapRequest.getSoapHeader();
        SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext
                .getResponse();
        SoapHeader respheader = soapResponse.getSoapHeader();
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        Iterator<SoapHeaderElement> itr = reqheader.examineAllHeaderElements();
        while (itr.hasNext()) {
            SoapHeaderElement ele = itr.next();
            transformer.transform(ele.getSource(), respheader.getResult());
        }
        // process the request PayLoad
        GetOrderNumberResponse response = null;
        try {
            response = getOrderNumberService.executeRequest(request);
        } catch (CannotCreateTransactionException e) {
            logger.error(ErrorConstants.ERROR_E17);
            throw new ServiceException(ErrorConstants.ERROR_E17);
        }
        logger.info("Response Sent");
        return response;
    }

字符串

1zmg4dgp

1zmg4dgp2#

这可能只是你需要的答案的一半,但我认为你可以通过从messagecontext中获取(Saaj)SoapMessage来阅读soapheader,如下所示:

@PayloadRoot(
    localPart = "GetHiredCandidatesRequest", 
    namespace = DEFAULT_NAMESPACE
)
@ResponsePayload
public GetHiredCandidatesResponse getKandidaat (
    @RequestPayload GetHiredCandidatesRequest getCandidate,
    MessageContext messageContext) {

    SaajSoapMessage request = (SaajSoapMessage) messageContext.getRequest();
    SoapHeader header = request.getSoapHeader();

    GetHiredCandidatesResponse response = objectFactory.createGetHiredCandidatesResponse();
    response.getCandidate().addAll(
        candidateService.getHiredCandidates(
            getCandidate.getFrom(), 
            getCandidate.getTo()
        )
    );

    return response;
}

字符串
从版本2开始,你可以自动地在方法的签名中添加一些对象,就像我在这里添加MessageContext一样。例如,我已经使用它从soap消息中获取附件。您可能也可以使用AbstractSoapMessage的其他子类,因为getSoapHeder方法在该类中。
[编辑]顺便说一句:也许你也可以使用拦截器,因为那里提供了请求/响应。查看org.springframework.ws.soap.server.endpoint.interceptor包中的一些默认示例。[编辑]

toe95027

toe950273#

如果你使用jaxb marshaller的话就容易多了。如果body和header有不同的命名空间,这个工作就可以了。

import org.springframework.ws.soap.server.endpoint.annotation.SoapHeader;//@SoapHeader

@PayloadRoot(namespace = "http://body.namespace", localPart = "getRequest")
@ResponsePayload
public JAXBElement<GetResponse> getSomething(@RequestPayload JAXBElement<GetRequest> request
        //SOAPHeader is element name from header.xsd: <xsd:element name="SOAPHeader" type="tns:HeaderType"/>
        ,@SoapHeader(value = "{http://namespace.x.com/Header/}SOAPHeader") SoapHeaderElement headerElem
        ,MessageContext messageContext) {

    try {
        JAXBContext context = JAXBContext.newInstance(HeaderType.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        //get soapheader from request
        DOMSource headerSource = (DOMSource) headerElem.getSource();
        //unmarshall to HeaderType object, headerSource.getNode() because header has different namespace from body namespace
        //HeaderType is an xml annotated class
        HeaderType headerRequest = ((JAXBElement<HeaderType>) unmarshaller.unmarshal(headerSource.getNode(), HeaderType.class)).getValue();

        //get soapheader from response
        SaajSoapMessage soapResponse = (SaajSoapMessage) messageContext.getResponse();
        org.springframework.ws.soap.SoapHeader soapResponseHeader = soapResponse.getSoapHeader();

        //marshall headerRequest to response soapheader
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(headerRequest, soapResponseHeader.getResult());
    } catch (JAXBException e) {
        e.printStackTrace();
    }

    //create response body message
    ObjectFactory objectFactory = new ObjectFactory();
    JAXBElement<GetResponse> response = objectFactory.createGetResponse(new GetResponse());
    response.getValue().setErrorMessage("ok");

    return response;
}

字符串

unftdfkk

unftdfkk4#

我正在使用SoapUI设置我的头文件,并试图在我的Sping Boot Soap Web Service中检索它们。上面提到的方法对我没有帮助。但我能检索到他们作为MimeHeaders。

//You can simply inject MessageContext. It is Spring's MessageContext, so do not use Xml or Jakarta MessageContext. Only use Spring MessageContext
    @PayloadRoot(namespace="http://anyuriyouwant.com", localPart="getEmployeeRequest")
@ResponsePayload public JAXBElement<Employee> getEmpDetails(@RequestPayload JAXBElement<String>empCode, MessageContext messageContext){
        SaajSoapMessage request=(SaajSoapMessage) messageContext.getRequest();
        MimeHeaders headers=request.getSaajMessage().getMimeHeaders();
        String appId=headers.getHeader("your-header-name")[0];
        System.out.println("App Id from headers: "+appId);
    }

字符串

相关问题