May 08, 2014

Web Services Interview Questions And Answers

What is a Webservice?
A web service is a collection of open protocols and standards used for exchanging data between applications or system over the World Wide Web (WWW).

Advantages of Webservices?
  • Because in web services communication is via XML so they are not tied to any one operating system or a programming language. So a Java can talk to .Net or Perl, UNIX applications can talk to Windows applications. Thus, web services allow different applications from different sources to communicate with each other that too without writing time-consuming custom code.
  • Loosely Coupled: Each service exists independently of the other services that make up the application. Individual pieces of the application to be modified without impacting unrelated areas.

What are key roles in a web service?
There are three major roles within the web service architecture:

  • Service Provider is the provider of the web service. The service provider implements the service and makes it available on the Internet.
  • Service Registry is a way in which web services are formally published. The service registry is based on the UDDI specification and reflects information about services provided by the service provider. The service registry provides a service requester with a Web Services Description Language (WSDL) service description and a Uniform Resource Locator (URL) that points to the service itself.
  • Service Requestor is the consumer of a web service and uses the service registry to gain information about, and access to, a web service.
What are the components of Web Services
The basic web services platform is XML + HTTP. All the standard web services work using the following components
  • SOAP : It stands for Simple object access protocol. It is protocol to exchange information using request and response in XML format over transport protocol such as HTTP, SMTP etc. SOAP is simple and extensible, and is platform and language independent
  • UDDI (Universal Description, Discovery and Integration) :  It is an XML-based standard for describing, publishing, and finding web services. UDDI uses WSDL to describe interfaces to web services and is platform independent, open framework. UDDI can communicate via SOAP, CORBA, and Java RMI Protocol.
  • WSDL (Web Services Description Language) : It is an XML file that describes the technical details of how to implement a web service, more specifically the URI, port, method names, arguments, and data types. WSDL is the language that UDDI uses.
What are the advantages and disadvantages of SOAP web services?
Advantages :-
  • WS Security: SOAP defines its own security known as WS Security.
  • Language and Platform independent: SOAP web services can be written in any programming language and executed in any platform.
Disadvantages :-
  • Slow: SOAP uses XML format that must be parsed to be read. It defines many standards that must be followed while developing the SOAP applications. So it is slow and consumes more bandwidth and resource.
  • WSDL dependent: SOAP uses WSDL and doesn't have any other mechanism to discover the service.
What is JAX-WS?
JAX-WS stands for Java API for XML Web Services. JAX-WS is standard XML based java API which is used to create SOAP web services.


What is 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.

What are some important annotations for JAX-WS?

@WebService, @WebMethod, @SOAPBinding

What do you mean by end point in terms of SOAP?
End point is nothing but URL which other application can use to access it.
e.g : http://localhost:8080/WS/MYWebService

How can you access WSDL for web service?
You need to put ?wsdl at the end of end point URL.
e.g :  http://localhost:8080/WS/MYWebService?wsdl

Can we maintain user session in web services?
Web services are stateless so we can’t maintain user sessions in web services.

Types of Web Services..

Web services are of two kinds: Simple Object Access Protocol (SOAP) and Representational State Transfer (REST).

SOAP Web service..
  • SOAP brings it’s own protocol and focuses on exposing pieces of application logic (not data) as services, that means SOAP web services exposes operations.
  • SOAP is focused on accessing named operations, where each operation implement some business logic through different interfaces. 
  • It defines a standard communication protocol specification for XML-based message exchange. SOAP  uses different transport protocols, such as HTTP and SMTP.
  • The standard protocol HTTP makes it easier for SOAP model to tunnel across firewalls and proxies without any modifications to the SOAP protocol.
  • SOAP has a drawback, it can sometimes be slower than middleware technologies like CORBA or ICE due to its verbose XML format.
  • SOAP requires less plumbing code than REST services design, (i.e., transactions, security, coordination, addressing, trust, etc.) Most real-world applications are not simple and support complex operations, which require conversational state and contextual information to be maintained. With the SOAP approach, developers need not worry about writing this plumbing code into the application layer themselves.
  • SOAP Web services (such as JAX-WS) are useful in handling asynchronous processing and invocation.
  • SOAP supports several protocols and technologies, including WSDL, XSDs, SOAP, WS-Addressin.
  • SOAP was originally developed by Microsoft. It has been around for a while and enjoys all of the benefits of long-term use. SOAP really isn’t as simple as the acronym would suggest.
ALSO READ: Java SOAP Web Service Example..

REST Web service..
  • REST are second generation Web Services. Unlike SOAP based web services, which require XML messages or WSDL service-API definitions, REST services communicate via HTTP. Thus, no middle ware is required for REST web services except HTTP support.
  • Benefit of using REST is that it describes a set of architectural principles by which data can be transmitted over a standardized interface (such as HTTP).
  • REST web services focuses on design rules for creating stateless services and does not contain an additional messaging layer. A client can access the resource using the unique URI and a representation of the resource is returned. With each new resource representation, the client is said to transfer state. While accessing RESTful resources with HTTP protocol, the URL of the resource serves as the resource identifier and GET, PUT, DELETE, POST and HEAD are the standard HTTP operations to be performed on that resource.
  • The RESTful web services are completely stateless. This can be tested by restarting the server and checking if the interactions are able to survive.
  • RESTful services provide a good caching infrastructure over HTTP GET method. This can improve the performance, if the data which the Web service returns is not altered frequently and is non dynamic in nature.
  • The service producer and service consumer need to have a common understanding of the context as well as the content being passed along as there is no standard set of rules to describe the REST Web services interface.
  • REST is particularly useful for restricted-profile devices such as mobile and PDAs for which the overhead of additional parameters like headers and other SOAP elements are less.
  • REST services are easy to integrate with the existing websites and are exposed with XML so the HTML pages can consume the same with ease. There is hardly any need to refactor the existing website architecture. This makes developers more productive and comfortable as they will not have to rewrite everything from scratch and just need to add on the existing functionality.
  • REST is the newcomer to the block. And REST based implementation is simple compared to SOAP. It seeks to fix the problems with SOAP and provide a truly simple method of accessing Web services.
Advantages of SOAP compared to REST..
  • SOAP is definitely the heavyweight choice for Web service access.
  • Language, platform, and transport independent (REST requires use of HTTP)
  • Works well in distributed enterprise environments (REST assumes direct point-to-point communication)
  • Standardized
  • Provides significant pre-build extensibility in the form of the WS* standards
  • Built-in error handling
  • Automation when used with certain language products
Advantages of REST compared to SOAP..
  • REST is easier to use for the most part and is more flexible.
  • No expensive tools require to interact with the Web service
  • Smaller learning curve
  • Efficient because SOAP uses XML for all the messages whereas REST can use smaller message formats.
  • Fast as no extensive processing required.
  • Closer to other Web technologies in design philosophy.
What is the difference between a REST web service and a SOAP web service?
  •  SOAP is language, platform and transport independent. REST works only over HTTP.
  • REST supports different formats like text, JSON and XML while SOAP only supports XML;
  • REST works with resources, each unique URL is some representation of a resource while SOAP works with operations, which implements some business logic through different interfaces.
  • SOAP web services are hard to maintain, any change in WSDL contract requires us to create client stubs again and then make changes to client code. Where as, REST web services are easy to maintain when compared to SOAP, a new method can be added without any change at client side for existing resources.
  • SOAP web services can be tested through programs or software such as Soap UI. REST can be easily tested through CURL command, Browsers and extensions such as Chrome Postman.
  • SOAP based reads cannot be cached, need to provide caching for SOAP while REST based reads can be cached.
What are main tags in WSDL?
The main structure of a WSDL document:
< definitions >
< types >
  data type definitions
< /types >

< message >
  definition of the data being communicated
< /message >

< portType >
  set of operations
< /portType >

< binding >
  protocol and data format specification
< /binding >
< /definitions >

Example:
< definitions >
< types >
  data type definitions
< /types >

< message >
< message name="getProductInfoRequest" >
  < part name="term" type="xs:string"/ >
< /message >

< message name="getProductInfoResponse" >
  < part name="value" type="xs:string"/ >
< /message >
< /message >

< portType name="productInfo" >
< operation name="getProductInfo" >
< input message="getProductInfoRequest"/ >
< output message="getProductInfoResponse"/ >
< /operation >
< /portType >

< binding name="productInfoPortBinding" type="tns:productInfo" >
< soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/ >
< operation name="getProductInfo" >
< soap:operation soapAction=""/ >
< input >< soap:body use="literal"/ >< /input >
< output >< soap:body use="literal"/ >< /output >
< /operation >
< /binding >

< service name="productInfoService" >
< port name="productInfoPort" binding="tns:productInfoPortBinding" >
< soap:address location="http://himanshushukla:8080/Testmart/productInfoService"/ >
< /port >
< /service >

< /definitions >


# The < portType > element defines a web service, the operations that can be performed, and the messages that are involved.
# 'productInfo' is the name of port. getProductInfo is the name of operation, which has getProductInfoRequest and getProductInfoResponse as input and output message.
# If there are multiple input arguments in a operation, then they are bundled together into a 'message'. Every method in WSDL, takes one 'message' as input and one as an output.
# The < message > elements define the parts of each message and the associated data types.
# < binding > tells, how a webservice accepts the request and gives response. 'transport' as 'http' denotes that this webservice accepts SOAP request over 'http'.
# < service > contais the list of ports.
# In short, service has a port and binding. Binding has a port type, which in turn has operation. Operation has input and output message.
How to customizing the WSDL using JAX-WS annotations?
Before customizing the WSDL, we need to undertand these two annotations:
@WebService:
# If we are annotating this over a class, it means that we are trying to mark the class as the implementing the Web Service, in other words Service Implementation Bean (SIB). Or we are marking this over an interface, it means that we are defining a Web Service Interface (SEI), in other words Service Endpoint Interface.
# When you annotate any class with @WebService (javax.jws.WebService), then  all public methods of this class are exported for our SOAP service

@WebMethod: This annotation can be applied over a method only. This specified that the method represents a web service operation.

Whenever we make any changes in the class (like renaming the class, package or method name) then WSDL is updated. When WSDL is updated, we need to generate new stubs, which is time consuming as each and every user of webservice need to generate it and need to update there code accordingly.

We can lock the WSDL by annotatations and customizing our java class.
@WebService
(name="ProductInfoName", portName="ProductInfoPortName",
serviceName="ProductInfoServiceName",
targetNamespace="ProductInfoTargetNS")


  • name is the name of webservice, it will change in portType name in WSDL. This make sure, even if we rename the class, or declare new class for webservice then 'name' attribute of 'portType' will not change.
  • serviceName: URL of WSDL is 'Webservice_ClassName+Service' in our case class name is 'ProductInfo' so the service name is 'productInfoService'. To lock this in WSDL, 'serviceName' is specified. It will change the url from 'productInfoService' to 'ProductInfoServiceName'. It also change the 'name' in 'service' tag.
  • targetNamespace: Namespace is used in XML World as an equivalent to the package in Java World. By declaring Namespace, you are grouping all the XML types together. 'targetNamespace' ensures, that all the types which are defined in the WSDL cannot be overriden by WSDL of same type. By default the value of 'targetNamespace' is autoderived from the package in which webservice is declared. for e.g if you have a webservice in package name is 'com.java.test' then targetNamespace would be 'test.java.com'.

@WebMethod (exclude=false,
action="fetchCategories",
operationName="fetchCategoriesOName")


  • exclude: when it is set to 'true' then that particular operation will not be published.
  • operationName will change the operation name.
What is @SOAPBinding and what is the difference between Document and RPC Style Web Services?
When you create Web Services using SOAP protocol, you follow either Document or RPC SOAP messaging style. By default its 'Document'.

The Document style indicates that the SOAP body contains a XML document which can be validated against pre-defined XML schema document.

RPC indicates that the SOAP message body contains an XML representation of a method call and uses the names of the method and its parameters to generate XML structures that represent a method’s call stack. The document/literal approach is easier because it simply relies on XML Schema to describe exactly what the SOAP message looks like while transmission.

-K Himaanshu Shuklaa..

No comments:

Post a Comment

RSSChomp Blog Directory