CodesJava

Easy learning with example program codes

web services interview questions and answers


What is a web service in java?

Web services refers to the standardized way of application-to-application interaction using the XML, SOAP, WSDL and UDDI open standards over internet. It represents an application component to exchange the information between two applications over the network.
 
A web service works on client server model where client applications can access web services over the network. To access a method or any other resource, client applications use endpoint URIs provided by web services.
 
Note: Web services are stateless and does not maintain any user session.
 

What are the components of web services?

XML:
Stands for Extensible Markup Language. It is used to tag the data.
SOAP
Stands for Simple Object Access Protocol. It is used to transfer the data.
UDDI:
Stands for Universal Description, Discovery and Integration. It is used for listing what services are available?
WSDL:
Stands for Web Services Description Language. It is used for describing the services available.

What are the advantages of web services?

Interoperability:
Interoperability is the ability of a system or a product to work with other systems or products without special effort. It is the most important benefit of Web Services. For example a web service created by java can be used by VB or .NET and vice versa.

Reusability:
As we discussed in previous topic a web service is a unit of managed code that can be remotely invoked over internet using HTTP requests i.e. Web services allows us to expose the functionality of our existing code over the network.

Low Cost of Communication:
As we discussed a web service can be invoked over internet using HTTP requests so we can use our existing low-cost internet for implementing web services.

Deployability:
Web Services are deployed over standard Internet technologies. This makes it possible to deploy Web Services even over the fire wall to servers running on the Internet on the other side of the globe. Also thanks to the use of proven community standards, underlying security (such as SSL) is already built-in.

Loose Coupling:
Web service code and server code are independent to each other which provides loose coupling.

What is interoperability in web services?

Interoperability:
Interoperability is the ability of a system or a product to work with other systems or products without special effort. It is the most important benefit of Web Services. For example a web service created by java can be used by VB or .NET and vice versa.

Note: In short, the code written for a web service will be treated as generic code and will be understood by all application.

Explain web service protocol stack layers

Web service protocol stack:
A web service protocol is a protocol stack that is used to define, locate, implement, and make Web services interact with each other.

A web service protocol stack consists of following four protocols:

  • Transport Protocol: It is responsible for transporting messages between network applications and includes protocols such as HTTP, SMTP, FTP etc.
  • Messaging Protocol: It is responsible for encoding messages in a common XML format so that they can be understood at either end of a network connection. It includes the protocol such as XML-RPC, WS-Addressing, and SOAP.
  • Description Protocol: It is responsible for describing the public interface to a specific Web service. It includes WSDL (Web Service Description Language).
  • Discovery Protocol: It is responsible for providing a way to publish and find web services over the web. It includes UDDI (Universal Description, Discovery, and Integration).

Explain web service architecture with diagram

web service protocol stack
A web service architecture consists of the following layers:
Service Provider: Service provider is responsible for creating the web service and making it accessible to the client applications over the internet.

Service Requestor: Service requestor is basically any client application which is accessing the web service.

Service Registry: Service registry is act as the centralized directory for the web services which helps to find web services for client applications.

Note: The ‘Publish’ interface of Service Registry is used by Service Provider to make the existing web services available to client applications. Service requestor use the information provided by the service registry to bind or invoke services.

What is UDDI and its significance?

UDDI stands for Universal Description, Discovery and Integration. It is used for listing what services are available? It acts as a directory and used for publishing and finding web services over the internet. It uses WSDL (Web Service Description Language).

What tools are used to test web services?

Web services refers to the standardized way of application-to-application interaction using the XML, SOAP, WSDL and UDDI open standards over internet. It represents an application component to exchange the information between two applications over the network.

Tools are used to test web services:

  • SoapUI
  • REST client
  • JMeter

What are different types of web services?

There are two types of web services:
SOAP web services:
SOAP stands for Simple Object Access Protocol. It is used to transfer the data. It is a XML-based messaging-layer protocol. SOAP can be used in combination with a variety of transport protocols like HTTP, SMTP, and JMS etc.
Note: SOAP is part of the set of standards specified by the W3C.
 
RESTful web services:
REST stands for REpresentational State Transfer. Unlike SOAP it is a web standards based architecture and not protocol. It uses HTTP protocol for data communication. REST provides the facility to represent a resource in various formats like text, JSON and XML.
Note: JSON is the most popular format.

What is soap webservices in java?

SOAP web services:
SOAP stands for Simple Object Access Protocol. It is used to transfer the data. It is a XML-based messaging-layer protocol. SOAP can be used in combination with a variety of transport protocols like HTTP, SMTP, and JMS etc.
Note: SOAP is part of the set of standards specified by the W3C.
 

what are the elements of soap message?

Elements of soap message:

Envelope: It is a mandatory element and used to define the start and the end of the message.
Header: It is an optional element which provides the information on authentication, encoding of data, or how a recipient of a SOAP message should process the message.
Body: It is a mandatory element and contains the XML data comprising the message being sent.
Fault: It is an optional element which provides information about errors that occur while processing the message.

What are the advantages of soap web services?

Advantages of soap web services:

  1. Using SOAP over HTTP allows for easier communication through proxies and firewalls than previous remote execution technology.
  2. SOAP is versatile enough to allow for the use of different transport protocols. The standard stacks use HTTP as a transport protocol, but other protocols are also usable e.g. SMTP and JMS.
  3. SOAP is platform independent.
  4. SOAP is language independent.
  5. SOAP uses WSDL document which provides contract and technical details of the web services for client applications without exposing the underlying implementation technologies.

What are the disadvantages of soap web services?

Disadvantages of soap web services:

  1. SOAP only supports XML and other lightweight formats like JSON are not supported.
  2. SOAP is based on contract between client and server applications which results into tight coupling.
  3. Client stub classes need to be generated again if there is any change in the server side contract.
  4. SOAP services cannot be tested easily in browse.
  5. Because of the verbose XML format, SOAP can be considerably slower than competing middleware technologies such as CORBA.
  6. When relying on HTTP as a transport protocol and not using WS-Addressing or an ESB, the roles of the interacting parties are fixed. Only one party (the client) can use the services of the other. Developers must use polling instead of notification in these common cases.
  7. Although SOAP is an open standard, not all languages offer appropriate support. Java, Curl, Delphi, PHP, .NET and Flex offer excellent SOAP integration and/or IDE support. Some Perl and Python support exists.
  8. SOAP clients do not hold any stateful references to remote objects.

what is wsdl in web services?

WSDL stands for Web Services Description Language. It is used for describing the services available. It provides the useful information for a web service like method name, port types, service end point, binding, method parameters etc.

What is difference between top down and bottom up approach in soap web services?

Top down approach:
In case of top down approach WSDL document is created first to establish the contract between web service and client, after that the code is written. This approach is also known as contract first approach.

It is difficult to implement because any thing we develop in the code should be confirmed against the already existing contract.

Bottom up approach:
In case of bottom up approach web services are developed first and then WSDL is generated based on web service code. This approach is also known as contract last approach.
 

Explain contract first approach in web service

In case of contract first approach WSDL document is created first to establish the contract between web service and client, after that the code is written. This approach is also known as top down approach.

It is difficult to implement because any thing we develop in the code should be confirmed against the already existing contract.
 

Explain contract last approach in web service

In case of contract last approach web services are developed first and then WSDL is generated based on web service code. This approach is also known as bottom up approach.

What is REST Web Services?

REST stands for REpresentational State Transfer. Unlike SOAP it is a web standards based architecture and not protocol. It uses HTTP protocol for data communication. REST provides the facility to represent a resource in various formats like text, JSON and XML.

Note: JSON is the most popular format.

Web services represents the resources that can be identified and accessed by their URIs. REST web services are lightweight and doesn’t follow any standard or set of rules like SOAP.

What are advantages of REST web services?

  1. RESTful web services are platform independent.
  2. RESTful web services are language independent.
  3. RESTful web services are fast.
  4. RESTful web services can use SOAP web services as the implementation.
  5. RESTful web services provides the facility to represent a resource in various formats like text, JSON and XML.
  6. It represents loosely coupled implementation as there is no contract required between server and client.

 

What is a resource in restful web services?

Normally in computer language, a resource refers to anything that can be stored on a computer and represented as a stream of bits: a document, a row in a database, or the result of running an algorithm.
 
In case of web services anything which is accessed by the URL you supply is a resource.
 
Note: URL is not a resource, it is just a label that identifies the resource. The XML, JSON, text etc are the representations of the resources.

What are the http methods used in restful web services

GET: It provides a read only access to a resource.
PUT: It is used to create a new resource.
DELETE: It is used to remove a resource.
POST: It is used to update an existing resource or create a new resource.
OPTIONS: It is used to get the supported operations on a resource.
HEAD: It returns HTTP header only, nobody.
 

SOAP REST
SOAP refers to Simple Object Access Protocol. It serves as a standard protocol for web service creation. REST refers to Representational State Transfer. It is an architectural style for web service creation.
SOAP is based on contract between client and server applications which results into tight coupling. It does not follow any contract between client and server that’s why loosely coupled.
For exposing business logic it uses service interfaces. It requires more bandwidth and resource. For exposing business logic it uses URI (Uniform Resource Identifiers). It requires less bandwidth and resource.
It support XML data format only. It support many data formats like Plain text, HTML, JSON, etc.
Java API for SOAP web service is JAX-WS. Java API for RESTFUL web service is JAX-RS.
SOAPUI can be used for testing SOAP web services. Browsers and extensions such as Chrome postman are used for testing REST web services.
It defines its own security and uses WSDL contract for binding web services and client application. It does not have its own security methods and does not defined any contract between client application and server.

 

What are different ways to test web services?

SOAP web services can be tested by software tools like SOAP UI. We can also test it programmatically by generating client stubs from WSDL.
 
We can test REST web services with program and curl commands. We can also test REST web services with browser extensions.
 

Can we maintain user session in web services?

No, we cannot maintain user session in web services because of web services stateless nature.

How to choose between soap and rest web services?

It depends upon the project requirement. Let’s discuss some points which can affect the choice of web service type.

  1. SOAP is more secured than rest. So choose SOAP if security matters like in banking projects.
  2. REST can be used for quick implementation because it can be created easily.
  3. SOAP can be used if only XML data is used otherwise use REST which support various formats like text, JSON and XML.

 

What is JAX-WS API?

JAX-WS stands for Java API for XML Web Services. It is a set APIs for creating web services and clients which communicates through XML format. It allows us to write message-oriented (Document-oriented) as well as Remote Procedure Call-oriented (RPC-oriented) web services.
 
Note: It is part of standard Java API and we don’t have to include anything else.

Explain annotations used in jax-ws api

  • @WebService
  • @SOAPBinding
  • @WebMethod

How to get wsdl file of a soap web service?

We can access WSDL document by appending ?wsdl to the SOAP endoint URL.
If SOAP endoint URL:

http://webservice.example:7001/test

Then use below address to access WSDL document

http://webservice.example:7001/test?WSDL

 

What is JAX-RS API?

JAX-RS refers to Java API for RESTful Web Services. It is Java API for creating REST web services. It is part of standard Java API and we don’t have to include anything else to use its annotations.

Note: JAX-RS provides several annotations to simplify the REST web services.

Explain some implementations of jax-rs api

  1. Jersey
  2. RESTEasy

 

What are the annotations used in JAX-RS API?

  • @Path: It is used to specify the relative path of class and methods.
  • @GET, @PUT, @POST, @DELETEand @HEAD: These annotations are used to specify the HTTP request type for a method.
  • @Produces, @Consumes: These annotations are used to specify the request and response types.
  • @PathParam: This annotation is used to bind the method parameter to path value by parsing it.

 

How to set status code in HTTP response?

The javax.ws.rs.core.Response class is used to set the status code in HTTP response.
 

Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki