简体   繁体   中英

Java Axis2 send SOAP response

I am trying to send a SOAP message after receiving a request. In Eclipse Mars 2, I generate Java Bean Skeleton from my .wsdl. The .wsdl only has one operation named send . After Axis2 generates all the classes and .xml files, it has one class called MyServiceSkeleton.java where the send() method is.

In that method, I am able to receive a SOAP request and extract the xml data with this code:

String xml = myServiceClass.getOMElement(null, OMAbstractFactory.getOMFactory()).toStringWithConsume();

I now want to make a new SOAP message and send a response either back to the requester or to a given ip address/url. Basically I have a XML string, which I made using the data from the request, and I want to turn it into a SOAP message then take the SOAP message and send it somewhere.

I'm assuming I have to make a request to the server I want to send it to. I tried this:

public void soapConnection() {
    try {
        SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
        SOAPConnection soapConnection = soapConnectionFactory.createConnection();

        // Send to server
        String url = "some ip address or url";
        soapConnection.call(createSOAPRequest(), url);
        soapConnection.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public SOAPMessage createSOAPRequest() throws Exception {
    MessageFactory messageFactory = MessageFactory.newInstance();
    SOAPMessage soapMessage = messageFactory.createMessage();
    SOAPPart soapPart = soapMessage.getSOAPPart();

    String uri = "some uri";
    SOAPEnvelope envelope = soapPart.getEnvelope();
    envelope.addNamespaceDeclaration("ns1", uri);

    // Take XML String and turn into envelope

    soapMessage.saveChanges();

    return soapMessage;     
}

I'm not sure how to code the part before soapMessage.saveChanges() . Any help would be appreciated.

You can send back a filed or an object to the caller by changing the service method return type( service method is the method exposed to external users who are consuming your services). For more details see the answer for this question SOAP fault handling in java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM