Sometime we need to change the Web Service WSDL URL dynamically at runtime when we call web service. The Web Service may deployed in different environments and you want depending on some conditions you will decide which version you should call.
In this example I will show you how to call SOAP Web Service progrmatically with dynamic wsdl url.
I used JDev 11.1.2.4.0 for developing this complete example.
1- Create a Simple Web Service:
I will create a simple web service has one function call "sayHello" this function will receive String and will return "Hello " + String.
- From JDeveloper create a custom application "e.g SimpleWebService" with one project "e.g SimpleWS" then create a class "e.g MyWebService.java" has one function sayHello as shown
- Right click on the MyWebService.java --> Create Web Service
- Go throw the opened wizard to create the web service. After finish creating the web service the application should be like:
- Right click on the MyWebService.java --> Test Web Service
Now the web service is running in my local machine with wsdl url
http://localhost:7101/SimpleWebService-SimpleWS-context-root/MyWebServicePort?wsdl
I will deploy it in another server and the web service wsdl url will be
http://weblogic2:8000/SimpleWebService-SimpleWS-context-root/MyWebServicePort?wsdl
- Now we finished creating Web Service and deployed it in 2 different environments with 2 different wsdl url.
2- Create a jar File for Calling The Web Service
To call this web service in other applications like ADF application or Java Application you should create jar file and use this jar file on this application.
- Create dummy application --> right click on the project --> New --> Web Service Client and Proxy
- Enter any of the Web Service wsdl url (I will enter the local running web service wsdl url) then press Finish
- The client class will open with code in case you want to test WS
- To create the jar file, right click on the project --> Deploy -->MyWebService-Client
- We will use the generated jar file on other application which will call this WS like ADF Application or any Java application
3- Call the The Web Service Programmatically With Dynamic WSDL URL
- Create new custom application "e.g. CallWebServiceDynamically" with java class "e.g. CallWS.java"
- Import jar file (which we created in the previous point) in the project
- Inside CallWS class write main method with this code
I will describe this code line by line:
QName qname = new QName("http://simplews/", "MyWebService");
QName will receive 2 parameters you can get these 2 parameters by running the Web Service WSDL in any browser and take the value of targetNamespace and name
URL wsdl1 = new URL("http://localhost:7101/SimpleWebService-SimpleWS-context-root/MyWebServicePort?wsdl");
In this line you write the desired web service WSDL URL
MyWebService_Service myWebService_Service = new MyWebService_Service(wsdl1, qname);
MyWebService myWebService = myWebService_Service.getMyWebServicePort();
These 2 lines you can get it from the dummy application client class (which generated when we try to generate jar file in the previous step)
But the first line is bit different because it will accept the wsdl url and gname
System.out.println("Web Service Output= " + myWebService.sayHello("Sameh"));
This is line for testing the Web Service and print the output on the log.
- To run the test application Right click on the CallWS --> run
You will find the web service output on the log
- Try to change the wsdl to point to the second deployed web service and run again
You can download the sample examples from here
In this example I will show you how to call SOAP Web Service progrmatically with dynamic wsdl url.
I used JDev 11.1.2.4.0 for developing this complete example.
1- Create a Simple Web Service:
I will create a simple web service has one function call "sayHello" this function will receive String and will return "Hello " + String.
- From JDeveloper create a custom application "e.g SimpleWebService" with one project "e.g SimpleWS" then create a class "e.g MyWebService.java" has one function sayHello as shown
- Right click on the MyWebService.java --> Create Web Service
- Go throw the opened wizard to create the web service. After finish creating the web service the application should be like:
- Right click on the MyWebService.java --> Test Web Service
Now the web service is running in my local machine with wsdl url
http://localhost:7101/SimpleWebService-SimpleWS-context-root/MyWebServicePort?wsdl
I will deploy it in another server and the web service wsdl url will be
http://weblogic2:8000/SimpleWebService-SimpleWS-context-root/MyWebServicePort?wsdl
- Now we finished creating Web Service and deployed it in 2 different environments with 2 different wsdl url.
2- Create a jar File for Calling The Web Service
To call this web service in other applications like ADF application or Java Application you should create jar file and use this jar file on this application.
- Create dummy application --> right click on the project --> New --> Web Service Client and Proxy
- Enter any of the Web Service wsdl url (I will enter the local running web service wsdl url) then press Finish
- The client class will open with code in case you want to test WS
- To create the jar file, right click on the project --> Deploy -->MyWebService-Client
- We will use the generated jar file on other application which will call this WS like ADF Application or any Java application
3- Call the The Web Service Programmatically With Dynamic WSDL URL
- Create new custom application "e.g. CallWebServiceDynamically" with java class "e.g. CallWS.java"
- Import jar file (which we created in the previous point) in the project
- Inside CallWS class write main method with this code
I will describe this code line by line:
QName qname = new QName("http://simplews/", "MyWebService");
QName will receive 2 parameters you can get these 2 parameters by running the Web Service WSDL in any browser and take the value of targetNamespace and name
URL wsdl1 = new URL("http://localhost:7101/SimpleWebService-SimpleWS-context-root/MyWebServicePort?wsdl");
In this line you write the desired web service WSDL URL
MyWebService_Service myWebService_Service = new MyWebService_Service(wsdl1, qname);
MyWebService myWebService = myWebService_Service.getMyWebServicePort();
These 2 lines you can get it from the dummy application client class (which generated when we try to generate jar file in the previous step)
But the first line is bit different because it will accept the wsdl url and gname
System.out.println("Web Service Output= " + myWebService.sayHello("Sameh"));
This is line for testing the Web Service and print the output on the log.
- To run the test application Right click on the CallWS --> run
You will find the web service output on the log
- Try to change the wsdl to point to the second deployed web service and run again
You can download the sample examples from here