Você está na página 1de 10

Web Services Interview Questions

In this series of interview questions about Architecture and Design, we cover interview questions related
to Architecture, Design, Design Patterns, Code Review, Performance and Load Testing, Web
Services, Restful Web Services, Security, Continous Integration and Java New Features

Interview Questions
Click Youtube Icon to Watch Now!!
1. What is a web service?
2. What are the important parts of a web service?
3. What are the advantages of Web Services?
4. What are the different types of web services?
5. What are SOAP Web Services?
6. What is a WSDL?
7. What is marshalling and unmarshalling? How do JAXB and XMLBeans help with this?
8. What are the java frameworks that you can use to build web services?
9. How do you handle security for Web Services?
10. What are the popular Java Specifications for Web services?
11. What is Service Oriented Architecture (SOA)?

What is a web service?

Simplest definition is a service offered over the web (internet) i.e. HTTP. Whenever we browse the
internet, we are making use of innumerable web services. Even the simplest attempt to load a
google.com website involves web services.
When we type in google.com in the browser, following steps happen in the background:

Step I : Browser invokes a get request on google.com.

Step II : google.com returns a HTTP response with the HTML content to show.

Step III : Browser understands the HTML content and renders it for the user.

Browser is invoking a get request and google.com server is responding with HTTP Response. This is a
great example of a web service.

What are the important parts of a web


service?
Lets use the above example to understand the three important parts of a web service.

Service Provider : Google.com is the service provider. Handles the request and sends a response
back.

Service Consumer : Browser is the service consumer. Creates Request. Invokes Service.
Processes the Response.

Data Exchange Format : In this example, Data Exchange is done over HTTP protocol. Request is
HTTP request and Response is HTTP Response. Data exchange format can be something else as
well. SOAP (in case of SOAP web services) and JSON (most RESTful services).

What are the advantages of Web Services?

Re-use : Web services avoid the need to implement business logic repeatedly. If we expose a web
service, other applications can re-use the functionality

Modularity : For example, tax calculation can be implemented as a service and all the applications
that need this feature can invoke the tax calculation web service. Leads to very modular application
architecture.

Language Neutral : Web services enable communication between systems using different
programming languages and different architectures. For example, following systems can talk with
each other : Java, .Net, Mainframes etc.

Web Services are the fundamental blocks of implementing Service Oriented Architecture in an
organization.

What are the different types of web


services?
Web services are usually classified based on the data exchange format. There are two popular styles of
web services.

SOAP Web Services : All web services using SOAP format (we will discuss this in detail later) are
called SOAP web services.

RESTful Web Services : All web services satisfying the Rest Architectural Constraints (we will
discuss this in detail too) are called RESTful Web Services.

What are SOAP Web Services?

In SOAP web services, data exchange (request and responses) happens using SOAP format. SOAP is
based on XML.
SOAP format defines a SOAP-Envelope which envelopes the entire document. SOAP-Header (optional)
contains any information needed to identify the request. Also, part of the Header is authentication,

authorization information (signatures, encrypted information etc). SOAP-Body contains the real xml
content of request or response.
All the SOAP web services use this format for exchanging requests and responses. In case of error
response, server responds back with SOAP-Fault.

What is a WSDL?
WSDL defines the format for a SOAP Message exchange between the Server (Service Provider) and the
Client (Service Consumer).
A WSDL defines the following

What are the different services (operations) exposed by the server?

How can a service (operation) be called? What url to use? (also called End Point).

What should the structure of request xml?

What should be the structure of response xml?

What is marshalling and unmarshalling?


How do JAXB and XMLBeans help with
this?

SOAP web services use SOAP based XML format for communication. Java applications work with beans
i.e. java objects. For an application to expose or consume SOAP web services, we need two things

Convert Java object to SOAP xml. This is called Marshalling.

Convert SOAP xml to Java object. This is called Unmarshalling.

JAXB and XMLBeans are frameworks which enable use to do marshalling and unmarshalling easily.

What are the java frameworks that you can


use to build web services?
One frequently used framework is Spring Web Services (also called Spring WS).
With Java EE 6 and Java EE 7, there is little need for using an external framework, as lot of web service
needs are built into them. So, it is a difficult choice to make.

How do you handle security for Web


Services?
Security for web services has to be handled at two levels : Transport level and Application level.

At transport level, SSL is used to exchange certificates (HTTPS). This ensures that the server
(service producer) and client (service consumer) are mutually authenticated. It is possible to use
one way SSL authentication as well.

At the application level, security is implemented by transferring encrypted information (digital


signatures, for example) in the message header (SOAP Header). This helps the server to
authenticate the client and be confident that the message has not been tampered with.

What are the popular Java Specifications


for Web services?
Popular java specifications for SOAP web services is JAX-WS. Rest Web service Specifications are JAXRS.

What is Service Oriented Architecture


(SOA)?

Service Oriented is an architectural style where applications are built on top of language neutral, loosely
coupled, independent, reusable components.
Major components of SOA are

User Interface Layer : User Interface for web/desktop applications.

Business Process Layer : This represents the layer where the use cases are choreographed on top
of the Services.

Services Services and components used to build the services, such as various frameworks

All important questions regarding Restful web services are covered in this
- See more at: http://www.javainterview.in/p/web-services-interviewquestions.html#sthash.oqYACjy4.dpuf

REST Web Services Interview


Questions
In this series of interview questions about Architecture and Design, we cover interview questions related
to Architecture, Design, Design Patterns, Code Review, Performance and Load Testing, Web
Services, Restful Web Services, Security, Continous Integration and Java New Features

Interview Questions
1. What is a REST Web Service?
2. What are important constraints for a RESTful Web Service?
3. What is Richardson Maturity Model?
4. What are the best practices in designing RESTful APIs?
5. What are the best practices in using HTTP methods with Restful Web Services?
6. Can you explain a little bit about JAX-RS?
7. What are the advantages of Restful web services?
8. What is the difference between REST and SOAP Based Services?

What is a REST Web Service?


Click Youtube Icon to Watch Now!!
There are a set of architectural constraints (we will discuss them shortly) called Rest Style Constraints.
Any service which satisfies these constraints is called RESTful Web Service.
There are a lot of misconceptions about REST Web Services : They are over HTTP , based on JSON etc.
Yes : More than 90% of RESTful Web Services are JSON over HTTP. But these are not necessary
constraints. We can have RESTful Web Services which are not using JSON and which are not over
HTTP.

What are important constraints for a


RESTful Web Service?
The five important constraints for RESTful Web Service are

Client - Server : There should be a service producer and a service consumer.

The interface (URL) is uniform and exposing resources. Interface uses nouns (not actions)

The service is stateless. Even if the service is called 10 times, the result must be the same.

The service result should be Cacheable. HTTP cache, for example.

Service should assume a Layered architecture. Client should not assume direct connection to
server - it might be getting info from a middle layer - cache.

What is Richardson Maturity Model?


Richardson Maturity Model defines the maturity level of a Restful Web Service. Following are the different
levels and their characteristics.

Level 0 : Expose SOAP web services in REST style. Expose action based services
(http://server/getPosts, http://server/deletePosts, http://server/doThis, http://server/doThat etc) using
REST.

Level 1 : Expose Resources with proper URIs (using nouns). Ex: http://server/accounts,
http://server/accounts/10. However, HTTP Methods are not used.

Level 2 : Resources use proper URI's + HTTP Methods. For example, to update an account, you do
a PUT to . The create an account, you do a POST to . Uris look like posts/1/comments/5 and
accounts/1/friends/1.

Level 3 : HATEOAS (Hypermedia as the engine of application state). You will tell not only about the
information being requested but also about the next possible actions that the service consumer can
do. When requesting information about a facebook user, a REST service can return user details
along with information about how to get his recent posts, how to get his recent comments and how
to retrieve his friends list.

What are the best practices in designing


RESTful APIs?

While designing any API, the most important thing is to think about the api consumer i.e. the client
who is going to use the service. What are his needs? Does the service uri make sense to him?
Does the request, response format make sense to him?

In Rest, we think Nouns (resources) and NOT Verbs (NOT actions). So, URIs should represent
resources. URIs should be hierarchical and as self descriptive as possible. Prefer plurals.

Always use HTTP Methods. Best practices with respect to each HTTP method is described in the
next question.

What are the best practices in using HTTP


methods with Restful Web Services?

GET : Should not update anything. Should be idempotent (same result in multiple calls). Possible
Return Codes 200 (OK) + 404 (NOT FOUND) +400 (BAD REQUEST)

POST : Should create new resource. Ideally return JSON with link to newly created resource. Same
return codes as get possible. In addition : Return code 201 (CREATED) is possible.

PUT : Update a known resource. ex: update client details. Possible Return Codes : 200(OK)

DELETE : Used to delete a resource.

Can you explain a little bit about JAX-RS?


JAX-RS is the JEE Specification for Restful web services implemented by all JEE compliant web servers
(and application servers).
Important Annotations:

@ApplicationPath("/"). @Path("users") : used on class and methods to define the url path.

@GET @POST : Used to define the HTTP method that invokes the method.

@Produces(MediaType.APPLICATION_JSON) : Defines the output format of Restful service.

@Path("/{id}") on method (and) @PathParam("id") on method parameter : This helps in defining a


dynamic parameter in Rest URL. @Path("{user_id}/followers/{follower_id}") is a more complicated
example.

@QueryParam("page") : To define a method parameter ex: /users?page=10.

Useful methods:

Response.OK(jsonBuilder.build()).build() returns json response with status code.

Json.createObjectBuilder(). add("id",user.getId()); creates a user object.

What are the advantages of Restful web


services?

Lightweight : Easy to consume from mobile devices also.

Easy to expose : Little or no restrictions on output format and communication protocol.

Most Restful services use HTTP protocol : Entire web is based on HTTP and is built for efficiency of
HTTP. Things like HTTP caching enable Restful services to be effective.

High Performance : Less xml & soap overhead and More caching enable Restful services to be
highly performant.

What is the difference between REST and


SOAP Based Services?
First of all, REST is a set of architectural principles defining how a RESTful service should look look like.
SOAP is a message exchange format. SOAP defines the structure of message to exchanged. How
should the header be? How should the request content be? So, there is no real comparison between
REST and SOAP.
To get a real comparison, I compare two popular implementation of these concepts.

Restful Sample Implementation : JSON over HTTP

SOAP Sample Implementation : XML over SOAP over HTTP

All comparison is between the Sample Restful and SOAP implementations described above.

REST is built over simple HTTP protocol. SOAP services are more complex to implement and more
complex to consume.

REST has better performance and scalability. REST reads can be cached, SOAP based reads
cannot be cached.

REST permits many different data formats (JSON is the most popular choice) where as SOAP only
permits XML.

SOAP services have well defined structure and interface (WSDL).

SOAP is based on well defined standards (WS-Security, WS-AtomicTransaction and WSReliableMessaging).

- See more at: http://www.javainterview.in/p/rest-web-services-interviewquestions.html#sthash.cuJyYooA.dpuf

Você também pode gostar