Tuesday 14 February 2017

WebServices

WebServices : To communicate two systems through networks.


SOAP : Simple Object Access Protocol based on XML it will process the services.
REST : REpresentational State Transfer can access over the networks.


Types of web services : 

There are two types of web services:

JAX-RPC based web services, in which a Service Provider publishes the service definition using a Web Services Definition Language (WSDL) and the Service Consumer sends a serialized XML message wrapped in a SOAP envelop across the wire.

JAX-RS based web services, in which a Service Provider publishes the Resource name that can be used to consume the service, and the Service Consumer uses stateless operations from the HTTP protocol and sends requests and receives response messages. Because of the stateless nature of the operations, web services are called Representational State Transfer (REST) services. The message payload can either be in XML or JSON format.

SOAP
REST
            1.   SOAP stands for simple object  standard protocol.
      1.  Rest follows Representational Architecture.
2. SOAP Permits only XML based data.
2. Rest accepts JSON, Text, XML and HTML data.
3. SOAP can’t use Rest services
3. Rest can use the SOAP Services.
4. SOAP uses Service interface to expose the logic.
4. Rest uses URI to expose the Business logic.


SOAP Webservices :
Proc      : 1. WSDL Doc provides Technical document to the client application.  
               2. Uses xml data as payload so we can easily read & access the data.

Cons     :  1. Tightly coupled with client & server.
               2. Otherthan xml formats wont work.
               3. Slow
               4. Can't be tested in other browsers.


Restful Webservices :

Adv       : 1. Light weight
               2. Loosely coupled
               3. Can tested in browsers.
               4. Fast compare to SOAP.

Dis Adv : 1. Sessions cant be maintained
               2. Since its uses http protocal, there cant be asynchronous calls.


Rest Methods

@GET     : Uses to fetch the data 
@POST   : Uses to create a resource and post (Save) the data.
@PUT     : Uses to update existing resource and save the data.
@Delete : Used to delete the resource or data.
@Path    : Used to find the resource path.(Class level or method level)
@RequestParam: Used to send the request from controller. (Spring based annotation)
@QueryParam : Used to send the optional parameters to the method.
@PathParam : Used to send the mandatory parameters to the method.


Session maintenance : 

As per the REST architecture, a RESTful Web Service should not keep a client state on the server. This restriction is called Statelessness. It is the responsibility of the client to pass its context to the server and then the server can store this context to process the client's further request. For example, session maintained by server is identified by session identifier passed by the client.

Gson :

Gson is a class which is providing by Google to convert the Java objects to Json and vice versa.

Parsing Json to Java Objects :
By using fromJson(Json json, Object T) we can parse the json to object.

Parsing Java Object to Json :
By using toJson(Object T) we can parse the Java object to json.

Pretty Printing :
new Gson() prints compact Json. To format the into more readable in a text editor we will use Pretty Printing like below.
Gson gson = new GsonBuilder().setPrettyPrinting().create();


SpringBoot

SpringBoot SpringBoot Application :  Pros & Cons :  SpringBoot Application creation using spring.io :  SpringBoot Application Annotation...