简体   繁体   中英

Web service, dynamic address in WSDL address

I'm very new to web services, so sorry if I write a lot of wrong things...

I created a few java classes and generated the wsdl, so I have a bottom-up web service. I deployed everything (in an EAR) and called

  http://localhost:7159/chc2/services/WebServiceManager

to invoke the web service. It works, I obtain the results.

The problem is that I need to deploy the application on other servers, and obviously the first part of the url, the local ip won't be the same.

That url is declared by me in the WSDL with:

<wsdlsoap:address location="http://localhost:7159/chc2/services/WebServiceManager"/>

My question is: there is a way to get the ip part of the link in a dynamic way in the .wsdl? I've found on the net some ways to do it in Java, but I'm directly calling the .wsdl, not passing through java...I guess if there is a way to do that in the .wsdl.

The other configuration files I have are server-config.wsdd and web.xml.

Thank you :)

Some application servers will allow you to submit Web services containing WSDLs with dummy address location:

<soap:address location="REPLACE_WITH_ACTUAL_URL"/>

Upon deployment, they will replace this value with actual URL! Glassfish supports this feature for sure (actually, I think that you can put whatever you want in address location value, Glassfish will replace it automatically), and according to this link , JBoss also supports this 'feature'. HTH.

As I understand, your issue is: when you deploy the webservice on other servers how would you call the webservice?

Your servers obviously are part of nodes/a cluster. On a Java EE application server, for example on WebSphere, plugin-config.xml is where you can configure the incoming/outgoing http ports.

The IBM HTTP Web Server will be configured with your Application server through plugin-config.xml . This will expose your webserice as http://:port/webserviceURI. This is the URL which you will use to call the webservice independent of the application server's IP.

Generate wsdl through XSD dynamically using sws tag placing in [servler-name]-servlet.xml

<sws:dynamic-wsdl
        id="getemployeeDetails"
        portTypeName="EmpService"
        locationUri="/empService"
        targetNamespace="http://www.example.org/">
        <sws:xsd location="/WEB-INF/employee.xsd"/>

    </sws:dynamic-wsdl>

In this way you can have the url as dynamic when hosted on server

Regards

Anshul

this is the way we can dynamically change the wsdl location url

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class WsdlDynamicPath {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {
            changePath();
        } catch (ParserConfigurationException | SAXException | IOException | 
 TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void changePath() throws ParserConfigurationException, SAXException, IOException, TransformerException{
        String filepath = "path to wsdl\\demo.wsdl"; //Give your wsdl file path
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(filepath);

        Node address= doc.getElementsByTagName("soap:address").item(0);

        NamedNodeMap attr = address.getAttributes();
        Node nodeAttr = attr.getNamedItem("location");
        nodeAttr.setTextContent("new path to wsdl");

        // write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File(filepath));
        transformer.transform(source, result);
    }

}

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