Advanced Java Interview Questions – Set 05

Explain about addClass function

This function translates a Java class name into file name. This translated file name is then loaded as an input stream from the Java class loader. This addclass function is important if you want efficient usage of classes in your code.

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.

What tools do you use to test your Web Services

SoapUI tool for SOAP WS and the Firefox “poster” plugin for RESTFul services.

Explain about the id field

This id field corresponds to the surrogate key which is generated by the database. These fields are handled by the id field. Name attribute is used to specify the names of the field and it should correspond to the method name of getid. This also should correspond to long type and the values should be stored I the database in the long column.

What are the pros and cons of server side versus client side templating

Server Side Templating

  • Pros:
    • More search engine friendly.
    • Entire response message can be cached.
    • Can work without JavaScript
  • Cons:
    • Harder to mix and match different server side technologies. For example, retrieve data from both Java and Ruby based services
    • Harder to send content for multiple devices. E.g. browser, mobile devices, etc.

Client Side Templating

  • Pros:
    • Faster development and prototyping
    • Serves JSON data to multiple devices.
    • The responses are smaller as JSON data is less verbose.
    • Templates and JSON data can be cached.
    • Can work with multiple server side technologies like .Net, JEE, Ruby, etc
  • Cons:
    • Less search engine friendly
    • Older browsers may not work properly
    • Cross browser compatbility testing is required

What are the different styles of Web Services used for application integration

SOAP WS and RESTful Web Service

What are the different approaches to developing a SOAP based Web service

These two 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.

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

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.