Basics of Web Services

SOAP: Simple Object Access Protocol.
REST: Representational State Transfer.
WSDL: Web services description language
Bottom-up means you start with a Java method, and generate the WSDL from java.
Top-down means you start with a WSDL and then create Java class from WSDL.
UDDI: UDDI ( Universal Description, Discovery and Integration ) is no longer(or rarely) used.
UDDI is central repository where companies(many companies) place their document about their webservices. From there we have to get the WSDL file for that particular company. Now-a-days wsdl file can be directly accessed using url of that company.
-Service provider register its WSDL to UDDI and client can access it from UDDI. It is a directory service. Web services can register with a UDDI and make themselves available through it for discovery.So following steps are involved.
-Service provider registers with UDDI.
-Client searches for service in UDDI.
-UDDI returns all service providers offering that service.
-Client chooses service provider
-UDDI returns WSDL of chosen service provider.
-Using WSDL of service provider,client accesses web service.

Basic understanding of web services
Soap is XML based. Therefore we give input in XML and we get output in XML format in Soap.
While REST is not a protocol but an architectural style. In rest we give input in Uri and we get output in Plain text, HTML, XML, JSON etc.
Therefore REST based implementation is simple compared to SOAP and consumer understanding.
In Rest a new method can be added without any change at client side while for SOAP any change in WSDL requires us to create client stubs again and then make changes to client code.
Why Rest Services are preferred?
Because it is stateless, means that the server does not store any state about the client. That is where the ST in REST comes from, State Transfer. You transfer the state in request instead of having the server store it. Hence there is no load of state management. State dependencies limit and restrict scalability. The client holds session state.

Client Request:
When a user makes a request for a resource, for example:
Request: http://localhost:8080/users
Accept: application/json -->client accepting a response in JSON
Content-Type: application/xml --> data being sent in the request is XML

Content-Type is sent in each request which helps the service identify whether request is xml/json/other

@RequestBody and @ResponseBody: 

  • @RequestBody and @ResponseBody annotations are used to bind the HTTP request/response body with object in method parameter or return type. Behind the scenes, these annotation uses HTTP Message converters to convert the body of HTTP request/response to objects.
  • Using @RequestBody, Spring will try to bind the request body [which can be JSON/XML/Other] to object based on Content-Type header in Http request. If Jackson library is found in your classpath, then Spring will use this library.

As from Spring 4,
@RestController = @Controller + @ResponseBody

ResponseEntity:
ResponseEntity represents HTTP response. You can specify status code, headers, and body.

MediaType:
Specify the MediaType to be produced or consumed by that particular controller method, to further narrow down the mapping.


Whatever data/response we will get from the server is known as Resource.

Restful used 4 main HTTP methods
GET - Retrieve Data
POST- Create/Insert Data
PUT- Update Data
DELETE- Delete Data


Web services specifications and implementations:
Web Services are mainly of 2 types, SOAP [Simple Object Access Protocol] and REST [Representational state transfer] based services. We have different type of specifications to implement SOAP and REST services.

· JAX-RS provides the implementation of Restful web services.
Since it is a specification, other frameworks can be written to implement these specifications, and that includes Jersey from Oracle, Rest easy from Jboss, CXF from Apache.

· JAX-WS, Apache Axis 2 provides the implementation for SOAP

· Apache CXF provides implementation for SOAP and Restful services both.

To be more clear here is an example. In order to access any website we need browser. So we could view any website either in Chrome, Mozilla or IE. So in this case consider browser (implementation) as jersey, rest easy, Apache CXF and website as rest/soap web service.


Note: We can call web-service from Jsp as well as from java class

Why use web service? Web services vs web application
Soln:
1) Can't expose database to third party or we cannot give access to database. Hence we create web services
2) We cannot create web application to access third party data. Hence companies like flipkart,snapdeal provide WSDL file or Uri of their web service to access their products which we can display in our web application






3 comments:

  1. I was very pleased to find this site.I want to thank you for this great content!! I enjoyed every little bit of it and I have you bookmarked to check out new stuff you post.
    spring hibernate training in chennai
    java training in chennai velachery

    ReplyDelete
    Replies
    1. I am happy that the information in my blog was helpful to you.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete