Web Services - SPLessons

Web Services RESTful

Home > Lesson > Chapter 10
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Web Services RESTful

Web Services RESTful

shape Description

RESTful web services are worked to work best on the Web. Representational State Transfer is a building style that determines requirements, for example, the uniform interface, that if connected to a web service instigate alluring properties, for example, execution, versatility, and modifiability, that empower administrations to work best on the Web. In the REST engineering style, information and usefulness are considered assets and are gotten to utilizing Uniform Resource Identifiers (URIs), regularly connects on the Web. The assets are followed up on by utilizing an arrangement of straightforward, all around characterized operations. The REST compositional style compels an engineering to a customer/server model and is intended to utilize a stateless correspondence convention, commonly HTTP. In the REST engineering style, customers and servers trade representations of assets by utilizing an institutionalized interface and convention.

shape Conceptual Figure

The following is an image to understand how RESTful services will communicate with other database strategies.

Annotations

shape Description

The following are the list of available annotations in RESTfull web services.

shape Example

The following is an example by using JAX-RS @Path, @GET and @PathParam Annotations. HelloService.java [java]package com.splessons.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; @Path("/hello") public class HelloService{ @GET @Path("/{param}") public Response getMsg(@PathParam("param") String msg) { String output = "Jersey say : " + msg; return Response.status(200).entity(output).build(); } } [/java] web.xml [xml]<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.splessons.rest</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app> [/xml] index.html [html]<a href="rest/hello/splessons">Click Here</a> [/html] Now compile the code result will be as follows on the server, When click on the button following message will be displayed. [java]Jersey say : splessons [/java]

Summary

shape Key Points

  • The FormParam annotation is used to indicate the form parameters.
  • The CookieParam annoation is used to indicate the cookie parameter.
  • The HEAD annotation will provide the method to give the response for the request.