Java Web Services Interview Questions and Answers: Overview

Q. What are the different application integration styles?
A. There are a number of different integration styles like

1. Shared database
2. batch file transfer
3. Invoking remote procedures (RPC)
4. Exchanging asynchronous messages over a message oriented middle-ware (MOM).


Q. How does a Java EE application integrates with other systems?
A. Using various protocols like HTTP(S), SOAP, RMI, FTP, proprietary, etc.

 

Q. What are the different styles of Web Services used for application integration?
A. SOAP WS and RESTful Web Service

Q. What are the differences between both SOAP WS and RESTful WS? 
A. 
  • The SOAP WS supports both remote procedure call (i.e. RPC) and message oriented middle-ware (MOM) integration styles. The Restful Web Service supports only RPC integration style.
  • The SOAP WS is transport protocol neutral. Supports multiple protocols like HTTP(S),  Messaging, TCP, UDP SMTP, etc. The REST is transport protocol specific. Supports only HTTP or HTTPS protocols.
  • The SOAP WS permits only XML data format.You define operations, which tunnels through the POST. The focus is on accessing the named operations and exposing the application logic as a service. The REST permits multiple data formats like XML, JSON data, text, HTML, etc. Any browser can be used because the REST approach uses the standard GET, PUT, POST, and DELETE Web operations. The focus is on accessing the named resources and exposing the data as a service. REST has AJAX support. It can use the XMLHttpRequest object. Good for stateless CRUD (Create, Read, Update, and Delete) operations.

             GET - read()
             POST - create()
             PUT - update()
             DELETE - delete()
  • SOAP based reads cannot be cached. REST based reads can be cached. Performs and scales better.
  • SOAP WS supports both SSL security and WS-security, which adds some enterprise security features like maintaining security right up to the point where it is needed, maintaining identities through intermediaries and not just point to point SSL only, securing different parts of the message with different security algorithms, etc. The REST supports only point-to-point SSL security. The SSL encrypts the whole message, whether all of it is sensitive or not.
  • The SOAP has comprehensive support for both ACID based  transaction management  for short-lived transactions and compensation based transaction management for long-running transactions. It also supports two-phase commit across distributed resources. The REST supports transactions, but it  is neither ACID compliant nor can provide two phase commit across distributed transactional resources as it is limited by its HTTP protocol.
  • The SOAP has success or retry logic built in and provides end-to-end reliability even through SOAP intermediaries. REST does not have a standard messaging system, and expects clients invoking the service to deal with communication failures by retrying.


  • Q. How would you decide what style of Web Service to use? SOAP WS or REST?
    A. In general, a REST based Web service is preferred due to its simplicity, performance, scalability, and support for multiple data formats. SOAP is favored where service requires comprehensive support for security and transactional reliability.

    The answer really depends on the functional and non-functional requirements. Asking the questions listed below will help you choose.

    • Does the service expose data or business logic? (REST is a better choice for exposing data, SOAP WS might be a better choice for logic).
    • Do the consumers and the service providers require a formal contract? (SOAP has a formal contract via WSDL)
    • Do we need to support multiple data formats?
    • Do we need to make AJAX calls? (REST can use the XMLHttpRequest)
    • Is the call synchronous or  asynchronous?
    • Is the call stateful or stateless? (REST is suited for statless CRUD operations)
    • What level of security is required? (SOAP WS has better support for security)
    • What level of transaction support is required? (SOAP WS has better support for transaction management)
    • Do we have limited band width? (SOAP is more verbose)
    • What’s best for the developers who will build clients for the service? (REST is easier to implement, test, and maintain)


    Q. What tools do you use to test your Web Services?
    A. SoapUI tool for SOAP WS and the Firefox "poster" plugin for RESTFul services.


    Q. What is the difference between SOA and a Web service?
    A.

    SOA is a software design principle and an architectural pattern for implementing loosely coupled, reusable and coarse grained services. You can implement SOA using any protocols such as HTTP, HTTPS, JMS, SMTP, RMI, IIOP (i.e. EJB uses IIOP), RPC etc. Messages can be in XML or Data Transfer Objects (DTOs).    

    Web service is an implementation technology and one of the ways to implement SOA. You can build SOA based applications without using Web services – for example by using other traditional technologies like Java RMI, EJB, JMS based messaging, etc. But what Web services offer is the standards based  and platform-independent service via HTTP, XML, SOAP, WSDL and UDDI, thus allowing interoperability between heterogeneous technologies such as J2EE and .NET.



    Q. Why not favor traditional style middle-ware such as RPC, CORBA, RMI and DCOM as opposed to Web services?
    A.

    The traditional middle-wares tightly couple connections to the applications and it can break if you make any modification to your application. Tightly coupled applications are hard to maintain and less reusable. Generally do not support heterogeneity. Do not work across Internet. Can be more expensive and hard to use.

    Web Services support loosely coupled connections. The interface of the Web service provides a layer of abstraction between the client and the server. The loosely coupled applications reduce the cost of maintenance and increases re-usability. Web Services present a new form of middle-ware based on XML and Web. Web services are language and platform independent. You can develop a Web service using any language and deploy it on to any platform, from small device to the largest supercomputer. Web service uses language neutral protocols such as HTTP and communicates between disparate applications by passing XML messages to each other via a Web API. Do work across internet, less expensive and easier to use.

    Q. What are the different approaches to developing a SOAP based Web service?
    A. 2 approaches.

    • The contract-first approach, where you define the contract first with XSD and WSDL and the generate the Java classes from the contract.
    • The contract-last approach where you  define the Java classes first and then generate the contract, which is the  WSDL file from the Java classes.

    Note: The WSDL describes all operations that the service provides, locations of the endpoints (i.e.e where the services can be invoked), and simple and complex elements that can be passed in requests and responses.

    Q. What are the pros and cons of each approach, and which approach would you prefer?

    A.

    Contract-first Web service


    PROS:

    • Clients are decoupled from the server, hence the implementation logic can be revised on the server without affecting the clients.
    • Developers can work simultaneously on client and server side based on the contract both agreed on.
    • You have full control over how the request and response messages are constructed -- for example, should "status" go as an element or as an attribute? The contract clearly defines it. You can change OXM (i.e. Object to XML Mapping) libraries without having to worry if the "status" would be generated as "attribute" instead of an element. Potentially, even Web service frameworks and tool kits can be changed as well from say Apache Axis to Apache CXF, etc
     
    CONS:

    • More upfront work is involved in setting up the XSDs and WSDLs. There are tools like XML Spy, Oxygen XML, etc to make things easier. The object models need to be written as well.
       
    • Developers need to learn XSDs and WSDLs in addition to just knowing Java.

     
    Contract-last Web service
     
    PROS:
    • Developers don't have to learn anything related to XSDs, WSDLs, and SOAP. The services are created quickly by exposing the existing service logic with frameworks/tool sets. For example, via IDE based wizards, etc.
        
    • The learning curve and development time can be smaller compared to the Contract-first Web service.
     
    CONS:
    •  The development time can be shorter to initially develop it, but what about the on going maintenance and extension time if the contract changes or new elements need to be added? In this approach, since the clients and servers are more tightly coupled, the future changes may break the client contract and affect all clients or require the services to be properly versioned and managed.
    •  In this approach, The XML payloads cannot be controlled. This means changing your OXM libraries could cause something that used to be an element to become an attribute with the change of the OXM.


    So, which approach will you choose?

    The best practice is to use "contract-first", and here is the link that explains this much better with examples -->  contract-first versus contract-last web services In a nutshell, the contract-last is more fragile than the "contract-first".  You will have to decide what is most appropriate based on your requirements, tool sets you use, etc.
     



    Note: More Java Web Services interview questions and answers including WSDL, SOAP, UDDI, JAXR, SAAJ, etc are covered in Java/J2EE Job Interview Companion with diagrams.


    More Web services Interview Questions and Answers

    Java Web Services Interview Questions and Answers: RESTful Web services
    Java Web Services Interview Questions and Answers: SOAP clients
    SOAP versus RESTful Web service -- comparison
    RESTFul Web Service URI conventions with Spring MVC examples

46 comments:

  1. "SOAP WS wupports both SSL security and WS-security" ,Nice article..but replace W with S in this statement.

    ReplyDelete
    Replies
    1. Looks like already corrected. By the way seems REST is getting more and more popular, at least I can say in interview. Its good to prepare few REST Web Service interview questions as well.

      Delete
    2. It depends where are you going for interview...

      Delete
  2. Fixed. Thanks for spotting the mistake.

    ReplyDelete
  3. Hi,

    Very nice one i ever found. BTW, I bought the eBook with 400 Q/A, but do not have latest after 2007. How can i get the update version of it?Thanks.
    Phani.

    ReplyDelete
  4. Hi Phani,

    I didn't have a chance to update it yet. I have updated some topics via this blog. Also, the core java career essentials has up to date info on core Java.

    ReplyDelete
  5. Arulkumaran Kumaraswamipillai
    I read your book
    Thank you very much!
    You've covered almost everything

    ReplyDelete
  6. Contract last is top down or bottom up approach?

    ReplyDelete
    Replies
    1. its, bottom up approach. First java classes and then WSDL.

      Delete
  7. The contract last is a bottom-up approach. The contract first or WSDL first is a top down approach

    ReplyDelete
  8. nice collection..wwwinterviewdotcom

    ReplyDelete
  9. Hi,

    I read your book, its too good and covered much about web services.

    ReplyDelete
  10. really awsum post....... well "http://www.dhung.com/job-interview-questions-answers/java-developer" is also provide question for java..................

    ReplyDelete
  11. we had developed a 10 services in server side production development services and we have to provide 5 services to the client while what should we do in how to prevent the remaing 5 services in server side

    ReplyDelete
  12. You need to elaborate a bit more on your question. Not clear as to what you are trying to do.

    ReplyDelete
  13. Good information webservices

    ReplyDelete


  14. Thanks for sharing, I will bookmark and be back again








    Anesthesia Billing Services

    ReplyDelete
  15. Very Nicely explained the core parts, differences and uses. Thanks a lot for sharing these details.

    ReplyDelete
  16. Very Nicely explained webservice

    ReplyDelete
  17. its very good explanation of web services concepts mainly RESTful vs SOAP bases webservice

    ReplyDelete
  18. Web Designing

    i Really bookmark reader great post wow ...

    ReplyDelete
  19. hi Arulkumaran,
    i need your help.
    am sending request thru SOAP to another application.
    they are getting my request. but when they are sending response its throwing following errors on their side.
    on my side only we have changed something..but their side they didnt do any changes frm long tym.
    Can you tell me what could be the problem?
    [WebContainer : 8] ERROR com.sbc.embus.adapter.soap.http.SoapAdapterServlet - SoapAdapterServlet encountered java.lang.ClassCastException while dispatching response
    java.lang.ClassCastException: progress.message.jimpl.Message incompatible with javax.jms.TextMessage
    at com.sbc.embus.adapter.soap.http.SoapAdapterServlet.doPost
    [WebContainer : 8] DEBUG com.sbc.embus.jxservice.dispatcher.util.Log4JLoggingHandler - PROPERTY embusErrorCode=500
    Thanks in advance

    ReplyDelete
  20. The first question itself is very good.

    ReplyDelete
  21. I read your Books. Your way of explaining things is pretty good.

    ReplyDelete
  22. Very nice artical

    ReplyDelete
  23. Hi I have bought your ebook. Wondering if you have updated book after 2007. I was looking for updated Spring,Web Service and java concurrency 1.5 and JSF questions. If I buy your 2009 version would i be able to get updated resources including stuff covered in this article? Thanks in advance.

    ReplyDelete
  24. This all updates are really useful for java web developers and also helpful for who are prepare to interview for Java based.
    Web Developer in Bangalore

    ReplyDelete
  25. All questions are helpful for when you go for the Interview.
    java interview questions

    ReplyDelete
  26. There are so many different styles to understand the concepts of Java web developing services.This is such a Great resource that you are providing. It gives in depth information. Thanks for this valuable information.

    ReplyDelete
  27. Already we have Apache HTTPS. Then why we are going for web services?

    ReplyDelete
  28. First of thanks for this nice blog. But I did't get in what context you have written "REST has AJAX support" . Do you means calling REST web service through AJAX. If so then, as per my knowledge It is possible to call SOAP web service through AJAX. Could you please clarify this.

    ReplyDelete
  29. Best explanations I have ever come across. Thanks for the post.

    ReplyDelete
  30. By far one of the best answer for the soap vs rest. I always stutter in an interview for thsi question, this gave me better insight of what it is. Thanks.

    ReplyDelete
  31. Hi ,
    About this sentence:

    "The SOAP WS supports both remote procedure call (i.e. RPC) and message oriented middle-ware (MOM) integration styles. The Restful Web Service supports only RPC integration style. "

    when we use web service with MOM (as transport layer) , the message will always contain a procedure to call , so it use RPC. So MOM and RPC are used together ?

    ReplyDelete
  32. It is great post.This blog provides the important java questions which is useful in the interview.The interested freshers can use this blog...
    Tutorial In Noida

    ReplyDelete
  33. very nice article..

    ReplyDelete
  34. thank u for gining information sir..... i am waiting for ur next updates...

    web-designing-training-institute-in-chennai

    ReplyDelete

5 recent Java developer posts