Web Services - Yash-777/ITextReports GitHub Wiki

Web Services work on client-server model where client applications can access web services over the network. Web services provide endpoint URLs and expose methods that can be accessed over network through client programs written in java, shell script or any other different technologies. Web services are stateless and doesn’t maintain user session like web applications.

Some of the advantages of web services are:

  • Interoperability: Web services are accessible over network and runs on HTTP/SOAP protocol and uses XML/JSON to transport data, hence it can be developed in any programming language. Web service can be written in java programming and client can be PHP and vice versa.
  • Reusability: One web service can be used by many client applications at the same time.
  • Loose Coupling: Web services client code is totally independent with server code, so we have achieved loose coupling in our application.
  • Easy to deploy and integrate, just like web applications.
  • Multiple service versions can be running at same time.

Note: For example, java application can interact with Java, .Net and PHP applications. So web service is a language independent way of communication.


  • REST[Representational State Transfer] is an architectural style to create web services. REST exposes methods through URIs, there are no technical details.
  • SOAP is acronym for Simple Object Access Protocol. SOAP uses WSDL to expose supported methods and technical details.

Soap only supports XML, where Rest supports different formats like text, JSON, XML, etc..,


Image result for JAX-RS API Java API for RESTful Web Services

JAX-RS: Java API for RESTful Web Services (JAX-RS) is the Java API for creating REST web services. JAX-RS uses annotations to simplify the development and deployment of web services. JAX-RS is part of JDK, so you don’t need to include anything to use it’s annotations.

There are two major implementations of JAX-RS API.

  1. Jersey: Jersey is the reference implementation provided by Sun. For using Jersey as our JAX-RS implementation, all we need to configure its servlet in web.xml and add required dependencies. Note that JAX-RS API is part of JDK not Jersey, so we have to add its dependency jars in our application.
  2. RESTEasy: RESTEasy is the JBoss project that provides JAX-RS implementation.
<servlet>
<servlet-name>Jersey Web Services</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
  <param-name>com.sun.jersey.config.property.packages</param-name>
  <param-value>list of packages contain Jersey Serviece classes</param-value>
</init-param>
<init-param>
  <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
  <param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Services</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>

Java:
	@GET
	@Path("/testcase")
	@Produces({"text/plain"}) | @Produces(MediaType.TEXT_PLAIN)
	getReqProducer(@QueryParam("id") String id) { ... }

Some of the important JAX-RS annotations are:

  • @Path: used to specify the relative path of class and methods. We can get the URI of a webservice by scanning the Path annotation value.
  • @GET, @PUT, @POST, @DELETE and @HEAD: used to specify the HTTP request type for a method.
  • @Produces, @Consumes: used to specify the request and response types.
  • @PathParam: used to bind the method parameter to path value by parsing it.
⚠️ **GitHub.com Fallback** ⚠️