Recently I have to pass JSON data to REST Service and did not have any simple Client handy. But created very simple Java program which read JSON data from file and sends it to REST service. Representational State Transfer (REST) has gained widespread acceptance across the Web as a simpler alternative to SOAP– and Web Services Description Language (WSDL)-based Web services. Key evidence of this shift in interface design is the adoption of REST by mainstream Web 2.0 service providers—including Yahoo, Google, and Facebook—who have deprecated or passed on SOAP and WSDL-based interfaces in favor of an easier-to-use, resource-oriented model to expose their services. In this article, Alex Rodriguez introduces you to the basic principles of REST.
Let’s start coding this:
1. Create RESTFul Web Service
2. Create RESTService Client
Another must read: Spring MVC Example/Tutorial: Hello World – Spring MVC 3.2.1
In Eclipse => File => New => Dynamic Web Project. Name it as “DemoWS”. Below tutorial also works with Tomcat 8.
Step-2 Create Deployment Descriptor File
If you don’t see web.xml (deployment descriptor) under WebContent\WEB-INF\ then follow these steps. Open web.xml and replace content with below contents:
Step-3 Convert Project to Maven Project
Follow this tutorial: http://crunchify.com/how-to-convert-existing-java-project-to-maven-in-eclipse/. Below is my pom.xml:
Create RESTFul service: DemoRESTService.java. Here we will create two services:
Deploy project DemoWS on Tomcat. Web project should be deployed without any exception.
Step-6 Verify REST service
Rest service should be accessible using this URL: http://127.0.0.1/DemoWS/api/verify
If you try to access http://127.0.0.1/DemoWS/api/demoService then you will see error code 405 - Method not allowed – which is valid response. As you can see it’s POST call and should expect some data with the request.
Next we will post below json into server:
Now let’s run Client Program by right click on DemoRESTServiceClient.java and you should see below two outputs:
1) in Tomcat Console
2) in Local Client Console
* The Java EE 5 Tutorial - Building Web Services with JAX-WS