In this Tutorial, we have provided the Most Frequently Asked Java Web Services Interview Questions & Answers with Examples & Explanation:
As experienced Java developers or software testers, it is essential that you should be well aware of web services developed using Java programming language and is expected that you have implemented or tested them during your tenure as a developer or test engineer.
In case you are trying to move to a more challenging role, we have come up with 25 most frequently asked interview questions and their appropriate answers asked on Java web services.
Let’s Explore!!
Table of Contents:
About Java Web Services
Web service is software that communicates between the client and the server using HTTP (HyperText Transfer Protocol) over the World Wide Web via XML based documents using SOAP, or JSON based documents using RESTful web services. A Java web service is a common platform to provide communication between different applications developed in different languages.
These services are platform-independent and can easily be implemented on the mainframe, personal computers, and mobile devices, which can be installed on operating systems viz. UNIX, Windows, Mainframe, Android, and iPhone. It supports communication protocols like HTTP, SMTP, and JMS.
Java Web Services Interview Questions
Q #1) What is Java web service?
Answer: These are software that communicates between the client and server using HTTP over the World Wide Web via XML based documents using SOAP, or JSON based documents using RESTful web services.
JAX-WS are Java API methods that are used for developing XML based web services, whereas JAX-RS is Java API methods used for developing RESTful web services.
Q #2) What are different types of Java web services?
Answer: SOAP (Simple Object Access Protocol) and RESTful (Representational State Transfer) services are the primary types of web services in Java.
Q #3) What are the advantages of using SOAP web services?
Answer: Simple Object Access Protocol (SOAP) based web services has the following advantages:
- SOAP is a lightweight stateless platform and is a language-independent protocol. It uses HTTP over the internet.
- It has Remote Procedure Call (RPC) support and uses XML format for data transfer between homogeneous or heterogeneous distributed applications.
- SOAP messages are communicated over different applications via a simple XML format.
- It is scalable due to the use of HTTP protocol that can communicate over the internet.
- SOAP offers data integrity and privacy by exposing components of application logic rather than data.
- SOAP is more suitable for payment gateways and telecom services where sensitive information like credit card information and personal information are dealt with.
Q #4) What are some of the advantages of using RESTful web services?
Answer: RESTful (Representational State Transfer) web services have the following advantages:
- REST protocol separates the User Interface from data storage and server, portable across various platforms.
- REST web services are language-independent as REST API methods can be implemented using various languages like PHP, Java, Python, and Node.js.
- It is a lightweight protocol that helps in data or information exchange with XML or JSON formats along with text, image, XML formats.
Q #5) What are the benefits of using RESTful web services over SOAP web services?
Answer: The benefits of using RESTful web services over SOAP web services can be explained in tabular format as below:
SOAP web services | REST web services |
---|---|
SOAP web services require users to follow strict rules in interacting with the server. | REST has the advantage of having increased speed, reduced bandwidth, increased formatting flexibility in comparison to SOAP. |
Implementation of SOAP web services is slower and results in delayed payload while working with SOAP messages. | REST web services work well with data and parse data faster as they are coupled with JSON. |
SOAP only allows XML format of data to be used in information transfer. | REST API can communicate via data, images, web addresses, tweets as well as blogs, in human-readable JSON format in addition to XML, HTML and plain text |
SOAP API uses Web services Definition Language (WSDL). | REST API uses Web Application Description Language (WADL). |
SOAP-based calls for request or response cannot be cached. | REST-based calls for request or response can be cached. |
SOAP supports WS-security and SSL (Secure Sockets Layer). | REST supports SSL and HTTPS(Hypertext Transfer Protocol Secure). |
Q #6) List and explain APIs for Java web services.
Answer: Java API methods used to develop web services are JAX-RPC, JAX-WS, and JAX-RS.
- JAX-RPC is an API method that is implemented to develop XML based web services using RPC. RPC stands for Remote Procedure Calls.
- JAX-WS is an API method that is implemented to develop XML based protocol viz. SOAP stands for Simple Object Access Protocol.
- JAX-RS is an API method that is implemented to develop RESTful web services. REST stands for Representational State Transfer, which uses XML, or JSON documents to send data.
Q #7) Please list commonly implemented frameworks for Java web services?
Answer: The commonly implemented Java web services frameworks are listed below:
- Apache Axis2
- Apache CXF
- Glassfish/Java web services Development Pack
- Jersey
- Jetty
- Jackson
- Matrix
- RESTEasy
- Web Services Interoperability Technology (WSIT)
- Web Services Interoperability Framework (WSIF)
- Dropwizard
Q #8) Explain annotations used in JAX-WS API to implement SOAP-based web services.
Answer: To create SOAP-based web services JAX-WS API methods are used. SOAP service can be defined in either RPC or document style.
For RPC style web service, a class with annotations should be created. This class declares methods, which are accessed by other applications for utilizing SOAP web services.
@WebService @SOAPBinding (style = SOAPBinding.Style.RPC) public interface UserUtility { @WebMethod public void addUser (User myuser); @WebMethod public Users getUsers (); }
Primary annotations used in the above code are:
(i) @WebService – annotation to declare service interface.
(ii) @WebMethod – annotation for each method exposed to other applications.
(iii) @SoapBinding – annotation indicates the RPC style of web service in the above code, it is
@SOAPBinding(style = SOAPBinding.Style.RPC) (Remote Procedure Calls)
(iv) To specify Document style service, it will be
@SOAPBinding(style = SOAPBinding.Style.Document)
Q #9) Explain annotations used in JAX-RS API to implement REST-based web services.
Answer: JAX-RS API methods are used to create RESTful web services with either Jersey or RESTEasy Framework.
@Path ("/src/auth_users") public class UserUtility { private static List<User> autho_users = new ArrayList<> (); @POST @Consumes (MediaType.APPLICATION_JSON) public Response addUser (User myuser) { autho_users.add (user); return Response.ok ().build (); } @GET @Produces (MediaType.APPLICATION_JSON) public List<User> getUsers () { return autho_users; } }
Primary annotations used in JAX-RS are explained below
- @Path – web service access path is defined here.
- @Produces – response type is specified here.
- @Consumes – request data type is specified here.
HTTP API methods have the following annotations in addition to above, @GET, @POST, @PUT, @DELETE, and @HEAD, etc.
Q #10) Explain JAXP API methods in building XML based SOAP web services.
Answer: JAXP is Java API methods for reading, manipulating, generating, and analyzing XML documents. By implementing these API methods, software developers can use them in e-commerce applications and web site publishing.
With JAXP API, processing XML documents becomes easy with DOM or SAX Parsers internally, thereby it can be utilized along with Java web service implementation such as JAX-WS which is XML based SOAP web services.
Q #11) How can you build web service using JAX-WS and JAXB API methods?
Answer: JAXB is a short form for Java Architecture for XML Binding. With JAXB XML as well as JSON, instance document is converted into Java Objects. It is Java standard for mapping Plain Old Java Objects (POJOs) to XML.
To implement web services using JAX-WS with JAXB. JAXB compatible parameters and return types are utilized to build business methods that face web service clients.
Q #12) Can you give examples of JAXB mapping for XML schema type to their corresponding Java data types?
Answer: The XML schema type to the Java data type in JAXB are given below:
XML Schema Type | Java Data Type |
---|---|
xsd: string | java.lang.String |
xsd: integer | java.math.BigInteger |
xsd: int | int |
xsd: long | long |
xsd: short | short |
xsd: decimal | java.math.BigInteger |
xsd: float | float |
xsd: double | Double |
xsd:dateTime | javax.xml.datatype.XMLGregorianCalendar |
Q #13) Can you walk me through steps to generate web service using the Jersey framework?
Answer: To create web services, we need to install JDK, and Apache Tomcat as our server in our client machine. We are using Eclipse as an IDE (Integrated Development Environment) to create a web services project.
After opening an Eclipse into Java EE i.e. Java Enterprise Edition, (in case you have any other default perspective, you can change the setting by following steps:
From Eclipse menu bar click menu ‘Window’, under this menu, select submenu ‘Perspective’, select an option ‘Open Perspective’, select ‘Other’ from the list of options, On clicking ‘Other’ will open a window with the title ‘Open Perspective’, from the list of perspectives; select ‘Java EE’, then click OK button.
The below image explains the steps to set Java EE perspective:
From Eclipse menu bar click menu ‘Window’, under this menu, select ‘Show View’ sub-menu, on clicking ‘Show View’ the list of options will display, select ‘Servers’. This will display the ‘Server’ tab with a note ‘No servers are available’, along with a link to create a new server.
Steps to add Servers tab:
Next, click on the link – it will open the New Server window, asking you to enter the server type. Enter Apache in the text field, this will list all Tomcat versions, in case you have Tomcat installed on your machine, then enter its version, click next and enter or browse the Tomcat installed path i.e. say in this case it is the installation path for Apache Tomcat is D:/tomcat.
Steps to map the Apache Tomcat server:
The selected Tomcat server will be displayed in the server tab, initially in the stopped stage. Right-click and select Start.
Click on First menu item from the Eclipse menu bar with a mouse, i.e. ‘File’, in the File menu, select ‘New’, or from keyboard select (Alt + Shift + N) keys together to go to New File option, this will display a list of options. Select ‘Maven Project’, window with a title ‘New Maven Project’ should open, click on ‘Next’ button, this will open another window, that asks the user to select an Archetype, There is a Filter type text field, type ‘jersey’.
In case, No archetype is listed under a jersey, from Eclipse menu bar click menu ‘Window’. Under this menu, select an option ‘Preference’ from the list, ‘Preferences’ window should open, select ‘Maven’ from the list in the left panel, this will display various choices in the right pane, select checkboxes for the multiple options listed this will list jersey archetype dependencies from the internet.
- Download Artifact Sources
- Download Artifact JavaDoc
- Download repository index updates on startup
- Update Maven projects on startup
You should get archetypes for filter jersey, select jersey-quickstart-webapp
Enter groupid, artifact id, & click finish. Maven Jersey project is created. On running the Tomcat server, the XML file displaying data values should get displayed on the browser.
Q #14) What is the importance of web services in software development.
Answer: Web service is important in many situations like,
- With web services, one can communicate/interact with any different software running on any platform, built-in any language.
- Various task-based workflows can be designed over the software that can be carried out by novice technical staff, to accomplish business-level analytics.
- Introducing a service interface that can be operated in a service environment to the legacy software applications, without changing the original application.
- Administrative and operational services that add reliability, accountability, security, providing versatility and usefulness, can be installed to monitor the features of the software.
Q #15) Explain layers in the web service protocol stack.
Answer: Web services follow a set of standards and protocols for data exchange and communication between the application.
Various layers of web service protocol stack are described below:
- Service transport – This layer focus on carrying or moving messages between applications. The protocols included in this layer are Hypertext Transfer Protocol ( HTTP), Simple Mail Transfer Protocol (SMTP), File Transfer Protocol (FTP), and Blocks Extensible Exchange Protocol (BEEP)
- XML messaging – This layer is built to convert the messages in a common XML format so that they are recognized between the sender and receiver. It includes XML-RPC and SOAP.
- Service description – This layer explains public interface, a point of interaction for two independent software, to the web service. The public interface in this layer is Web Service Definition Language (WSDL).
- Service Discovery – This layer converts services into a common registry, adding find/publish functionality. This layer is managed by Universal Description, Discovery, and Integration (UDDI).
Q #16) Explain the importance of security in web services.
Answer: To meet constantly evolving software requirements and keep in touch with customers and deliver the expected changes in this agile work environment, software companies have adopted Bring Your Own Devices (BYOD) policy and working remotely for their employees.
Employees connect their company’s repositories to verify requirements, share documents, access code base, and build integrate new functionalities into these codes, on a continuous base. It is very essential to secure these systems from being exposed to unauthorized personnel outside the organization.
The web services that deliver sensitive information such as financial or personal data about the company, client, or project details should be well protected against any virus or malware, by installing the latest security software that prevents attacks or hackers trying to access or destroy this data as well as connectivity.
It is essential to have proper authentication by valid employees and disconnecting unattended connected data sources, and FTPs or information on the cloud.
System administration or deployment team should constantly upgrade programs and operating systems, manage or monitor authentication privileges, configure Wi-Fi encryption with strict and strong password policy, making users change login credentials at regular intervals. The systems used under the BYOD policy get locked in case unattended for the predefined interval.
Q #17) What are the standards used in web services?
Answer: Web services standards include the following:
- Simple Object Access Protocol (SOAP) is a stateless protocol for transferring data in XML format between different applications via HTTP over the Internet. It is a platform and language independent XML based interface for web services between homogeneous and heterogeneous distributed applications.
- Message Transmission Optimization Mechanism (MTOM) is a standard that gives mechanisms for transferring binary data between different applications via web services over the Internet. It helps to encode, compress as well as remove binary data from the SOAP envelope. It can also attach binary data as well as additional references to the Multipurpose Internet Mail Extensions (MIME) package in the SOAP envelope.
- Hypertext Transfer Protocol (HTTP) is an application layer stateless protocol for transferring hypermedia documents like HTML documents over TCP/IP protocol. Various HTTP methods like GET are used for receiving responses and sending requests via the POST method. The most commonly used API based on HTTP is XMLHTTPRequest.
- Universal Description, Discovery, and Integration (UDDI) is a platform-independent, open framework specification to find, describe, and publish web services. UDDI uses Web Services Definition Language (WSDL) to explain interface to web services. It can communicate via SOAP, Common Object Request Broker Architecture (CORBA) which is a network protocol to communicate between different languages and platforms, and Java remote method invocation (RMI) protocol.
- Web Service Definition Language (WSDL) is an XML document that is used to generate test requests, assertions, and mock services to validate SOAP-based web service. WSDL files are in XML format which consists of web service locations and methods that are used by web services. WSDL file are composed of five main parts which are <types>, <messages>, <portType>, <binding> and <service>.
- Web service Discovery Tool (DISCO) is used to identify URLs of XML based web services. This is a tool that discovers and publishes discovery documents.
Q #18) What is the JAXB binding framework?
Answer: EXtensible Markup Language (XML) is used in web services as a standard for data transport, communication, and configuration. It converts data received in XML format into an object and vice-versa. Java Architecture for XML binding (JAXB) gives a mechanism or API methods to arrange (Marshal) Java objects into XML and XML into objects.
JAXB data binding process consists of following main tasks:
- Bind: This task carried out by JAXB schema compiler, binds, or joins XML schema to JAXB Java classes, these Java classes give access to Java Bean access methods (GET and SET).
- Unmarshal: This task is managed by the JAXB binding framework, converts XML documents into Java objects.
- Marshal: This task is managed by the JAXB binding framework, converts Java objects back to XML documents.
JAXB binding language helps in the declaration of custom binding as well as JAXB annotations specifications to control the conversion of data between XML and Java.
JAXB Annotations such as
- @XmlRootElement specifies the root element for an XML document.
- @XmlAttribute specify the attribute of the root element.
- @XmlElement specifies sub-element for the root element.
Q #19) Can you explain XML digital signature API methods?
Answer: Java XML digital signature API methods are used to create and validate XML signatures. XML signatures are used to secure data, message and signer authentication can be applied to any type of data XML or binary. It is a pluggable and extensible API and is based on Java Cryptography Service provider architecture.
Q #20) What are the six packages available in the XML digital signature API?
Answer: The six packages that comprise XML digital signature API are described below:
- javax.xml.crypto package consists of classes instrumental in XML cryptographic operations like generate XML signature or encrypt XML data.
- javax.xml.crypto.dsig package that has interfaces represented core elements in W3C XML digital signature specification, also contain XMLSignature class, by which developer can sign and validate XML digital signature.
- javax.xml.crypto.dsig.keyinfo package has interface which contains KeyInfo, structure recommended in W3C XML digital signature specification.
- javax.xml.crypto.dsig.spec package that comprise of interface and classes that represent input parameters for digest, signature that utilize in processing XML signatures.
- javax.xml.crypto.dom package contains classes specific to DOM for javax.xml.crypto package.
- javax.xml.crypto.dsig.dom package contains classes specific to DOM for javax.xml.crypto.dsig package.
Q #21) How many communication channels can be used in web services?
Answer: Web service communication channels are HTTP/POST, HTTP/GET, and SOAP.
- HTTP/POST protocol is used as a communication channel for secured mode information transferred between clients.
- HTTP/GET protocol is used to provide clients the privilege to view transferred data partially at the browser’s address bar.
- SOAP protocol is used to safely transfer sensitive/confidential data across different applications through web services.
Q #22) Explain in brief web service architecture and its roles.
Answer: Web service architecture assist the developer with steps and procedures that are required to accomplish creation and validate web service with three roles.
These three roles include:
- Service Provider who creates web services and provides its access to the client application who wants to use it.
- Service Requester is a client application that will use web services developed in any programming language.
- Service Broker (registry) is an application that allows access to the UDDI, which helps client application to locate the web service.
Q #23) What is the difference between API and web service?
Answer: API is an application programming interface that is part of the Java development kit, which provides a list of classes and methods utilized to develop programs to facilitate interaction between two applications so that they can communicate with each other.
Web service is the application features that allow communication between two different applications over a network via the HTTP protocol, with the help of web service description language in XML format.
Q #24) What are some of the examples of public REST API provided to access web services?
Answer: Google Maps provide public REST API key to their users to use their maps to locate and access locations or distance remaining etc. Users can access Google maps at various zoom levels like World, Continent, Country, City, Streets, and buildings.
Q #25) What is the difference between XML and JSON format?
Answer: JSON is less verbose (using only essential words) and lightweight, hence it is easier to read, write, and locate values from its structure. XML allows the developer to use metadata within tags and can handle mixed content better than JSON.
Conclusion
Web service is a software that communicates between the different applications using HTTP over the World Wide Web via XML based documents or JSON based documents.
Every possible area of Java web services, annotations used in API methods, frameworks, a protocol stack, and standards for creating web services has been asked as part of interview questions, with most technical answers to each of the questions.
We hope you have found the answers to the most frequently asked interview questions on ‘Java web services’. As often you should practice; refer and revise these questions and corresponding answers, you will be able to confidently give the right answers and will become successful in technical interviews.
All the best!!