Unmarshalling SOAP Messages to java / or simply pa

2019-06-08 01:27发布

问题:

I am facing this problem for over than one month , so i would be realy pleased by your help , in fact i am asking about a way that can let me parse a SOAP message (request) to can retrieve the needed information , such as the security information if there is any and informations from the body of the message


Thanks for answering me , but know i am dealing with another problerm which is the WS-SecurityPolicy and i have to finaly parse an xml file like this one : <?xml version="1.0" encoding="UTF-8"?>

    <sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
      <wsp:Policy>
        <sp:TransportToken>
          <wsp:Policy>
            <sp:HttpsToken RequireClientCertificate="false"/>
          </wsp:Policy>
        </sp:TransportToken>
        <sp:AlgorithmSuite>
          <wsp:Policy>
            <sp:Basic128/>
          </wsp:Policy>
        </sp:AlgorithmSuite>
        <sp:Layout>
          <wsp:Policy>
            <sp:Lax/>
          </wsp:Policy>
        </sp:Layout>
        <sp:IncludeTimestamp/>
      </wsp:Policy>
    </sp:TransportBinding>
    <sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy">
        <wsp:Policy>
            <sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient" />
      </wsp:Policy>
    </sp:SignedSupportingTokens>

'

knowing that this XML file is named Policy.xml and contains the rules of WS-SecurityPolicy, which must be present.

回答1:

It depends on what's inside your message. You've tagged your question with jaxb which makes me think that you have xml-serialized data inside soap message. If this is the case you could use JAXB unmarshaller to convert your message to an instance of Java class:

JAXBContext jbc = JAXBContext.newInstance("com.mypackage");
Unmarshaller um = jbc.createUnmarshaller();

JAXBElement<MyClass> element = um.unmarshal(parameterNode, MyClass.class);
MyClass data = element.getValue();


回答2:

I've found this code to unmarshall from a SOAPResponse Message to POJO class, I hope be helpful.

    //unmarshalling
    JAXBContext jc = JAXBContext.newInstance(MyPOJO.class);
    Unmarshaller um = jc.createUnmarshaller();
    MyPOJO output = (MyPOJO)um.unmarshal(soapResponse.getSOAPBody().extractContentAsDocument());


回答3:

i'm guessing you already have a soap object and want to parse the contents of the message.

// assumptions: soapMessage contains the SoapMessage
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        soapMessage.writeTo(baos);
        final InputSource inputSource = new InputSource(new StringReader(
            new String(baos.toByteArray())));
        final DocumentBuilderFactory dbFactory = DocumentBuilderFactory
            .newInstance();
        final DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        final Document doc = dBuilder.parse(inputSource);
        doc.normalize();
// after this use Xpath to process the soapMessage

if you have the soap message as a string then you can start from building the Document object using the string.