In my previous post I have shown how you can create a Restful web service, in this post I will show how you can call this web service programmatically.
Before start you should download the sample web service from here and run the web service.
Follow These Steps to call the web service:
1- From JDeveloper create new custom application
For example, Application Name: CallRestfullWSProgrammatically
2- In this application create new class
For example, Class Name : CallRestfulWS.java
3- In the project properties add 2 libraries
- jersey-client-1.18.jar
- jersey-core-1.18.jar
You can get these jar files from the uploaded sample application
4- Write this code for the main method
5- Run the main method (Right Click ---> Run)
Download the sample application (done using JDev 11.1.2.4.0) from here
Before start you should download the sample web service from here and run the web service.
Follow These Steps to call the web service:
1- From JDeveloper create new custom application
For example, Application Name: CallRestfullWSProgrammatically
2- In this application create new class
For example, Class Name : CallRestfulWS.java
3- In the project properties add 2 libraries
- jersey-client-1.18.jar
- jersey-core-1.18.jar
You can get these jar files from the uploaded sample application
4- Write this code for the main method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) | |
{ | |
try | |
{ | |
Client client = Client.create(); | |
WebResource webResource = client.resource("http://localhost:7101/RestfulApplication-Restful-context-root/jersey/hello"); // Web Service URL | |
MultivaluedMap queryParams = new MultivaluedMapImpl(); | |
queryParams.add("myName", "Sameh Nassar");// parameter name and value | |
ClientResponse response = webResource.queryParams(queryParams).get(ClientResponse.class); // for get method | |
System.out.println("response.getStatus()response.getStatus()= " + response.getStatus()); | |
if (response.getStatus() != 200) | |
{ | |
throw new RuntimeException("Failed : HTTP error code : " + response.getStatus()); | |
} | |
String output = response.getEntity(String.class); | |
System.out.println("Output from Web Servicve: " + output); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
} |
5- Run the main method (Right Click ---> Run)
Download the sample application (done using JDev 11.1.2.4.0) from here