Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to RESTful Webservices in JAVA


Published on

Introduction to RESTful Webservices in JAVA with Sample Code. Also presents , How to develop in Eclipse with Apache Tomcat v7 using Jersey.

Published in: Software
  • Have you ever used the help of ⇒ ⇐? They can help you with any type of writing - from personal statement to research paper. Due to this service you'll save your time and get an essay without plagiarism.
    Are you sure you want to  Yes  No
    Your message goes here

Introduction to RESTful Webservices in JAVA

  1. 1. What is REST ? • REpresentational State Transfer • Its an architectural style • Its Peculiar features are 1. Client-server 2. Stateless 3. Cached 4. Uniform interface 5. Layered system P.S.R Patnaik (CyberJungle.Org)
  2. 2. Components of REST Style Architecture • Resources are identified by uniform resource identifiers (URIs) • Resources are manipulated through their representations • Messages are self-descriptive and stateless • Multiple representations are accepted or sent P.S.R Patnaik (CyberJungle.Org)
  3. 3. Representation in REST • Resources are first-class objects – Indeed, “object” is a subtype of “resource” • Resources are retrieved not as character strings or BLOBs but as complete representations P.S.R Patnaik (CyberJungle.Org)
  4. 4. State • “State” in REST means application/session state • State is associated with the content transferred from client to server back to client • Thus any server/application can continue transaction from the point where it was left off P.S.R Patnaik (CyberJungle.Org)
  5. 5. REST • State in REST is transferred by exchanging messages between Server and Client • REST messages are usually a XML or JSON string containing information pertaining to state of application. • REST uses HTTP for STATE transfer and HTTP is the most RESTful protocol with GET , PUT , POST , DELETE operations !! P.S.R Patnaik (CyberJungle.Org)
  6. 6. RESTful Web Services • Now we know what is REST and its components , so what is RESTful webservice ? P.S.R Patnaik (CyberJungle.Org)
  7. 7. RESTful Webservice • A RESTful Web service follows four basic design principles: • Uses HTTP methods • Be stateless as far as possible. • Expose directory/folder structure-like URI/URL • Transfer XML, JavaScript Object Notation (JSON), or both. P.S.R Patnaik (CyberJungle.Org)
  8. 8. HTTP Methods and REST • GET: Reading access of the resource (idempotent). • PUT : Creates a new resource. (idempotent) • DELETE: Removes the resources. (idempotent) • POST: Updates an existing resource or creates a new resource. (idempotent) P.S.R Patnaik (CyberJungle.Org)
  9. 9. RESTful Webservice Using JAVA • JSR 311: JAX-RS: The JavaTM API for RESTful Web Services • JSR 311 : API for providing support for RESTful(Representational State Transfer) Web Services in the Java Platform. P.S.R Patnaik (CyberJungle.Org)
  10. 10. How to develop RESTful Webservice in Java • By using simple Java Servlet and conforming to REST standards and best practices • By using JSR 311 implementation like famous Jersey framework , Jackson (fasterxml), Resteasy, Apache CXF. P.S.R Patnaik (CyberJungle.Org)
  11. 11. RESTful Webservice Best Practices • Provide a distinct URI for each resource you wish to expose. • Use nouns in your URIs, they highlight the fact that resources are things and not actions. • Methods that map to GET should not change any data. • Methods that map to POST should change the data. • Make your service stateless. P.S.R Patnaik (CyberJungle.Org)
  12. 12. Designing RESTful Webservice • Define the resources the service will expose. A service exposes one or more resources that are organized as a tree. • Define what actions you want to be able to perform on each resource. • Map the actions to the appropriate HTTP verbs. • For example you want to create service to book ticket. For example, booking ticket /bookingticket/getavailability /bookingticket/bookticket /bookingticket/updateavailability P.S.R Patnaik (CyberJungle.Org)
  13. 13. Simple RESTful Java Webservice using Jersey and Eclipse Kepler Prerequisites • Eclipse Kepler • Jersey API ( • Apache Tomcat v7 P.S.R Patnaik (CyberJungle.Org)
  14. 14. Simple RESTful Java Webservice using Jersey and Eclipse Kepler • Create a Dynamic Web Project • Configure it to use Apache Tomcat v7 as Runtime. (ensure that tomcat runs on JDK 7) • Add Jersey libraries to Project’s Build Path and also lib directory of Web project • Add a package with name “cyberjungle” to the web project’s src directory. • Add a java file and name it as P.S.R Patnaik (CyberJungle.Org)
  15. 15. Simple RESTful Java Web service using Jersey and Eclipse Kepler • Add following code to the file package cyberjungle; import; import; import; import; @Path("/hello") public class HelloSimpleRestfulWS { @GET @Path("/{param}") public Response getMsg(@PathParam("param") String msg) { String output = "Hello !! How are you " + msg.toUpperCase(); return Response.status(200).entity(output).build(); } } P.S.R Patnaik (CyberJungle.Org)
  16. 16. Simple RESTful Java Web service using Jersey and Eclipse Kepler • Addweb.xml to the projects WEB-INF <web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation=""> <display-name>Simple Restful Web Application Using Jersey</display-name> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>cyberjungle</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/restfulws/*</url-pattern> </servlet-mapping> </web-app> P.S.R Patnaik (CyberJungle.Org)
  17. 17. Simple RESTful Java Web service using Jersey and Eclipse Kepler • Run the application • Browse the following URL http://localhost:8080/SimpleRESTfulJavaWebS ervice/restfulws/hello/psrpatnaik • The output would look like. Hello !! How are you PSRPATNAIK P.S.R Patnaik (CyberJungle.Org)
  18. 18. Reference Resources • IBM Developers Work. • Oracle RESTfulWeb Services Developer's Guide • Jersey User Guide. P.S.R Patnaik (CyberJungle.Org)