Q: What is the Contract First and Service First Approach?
Contract-first is where you create the WSDL, and then you can create the implementation from this, and since the WSDL is your contract, this would be contract-first.
Service First or Contract-last or Code-First is where the WSDL is created from the source code, or implementation, so it will most likely be generated by a tool rather than created by the developer.
Q: Which is the better approach to web services – contract first or contract last?
Contract-first is the generally accepted ‘best practice.’.Both the producer and consumer of the service exactly what is needed and what is expected. It empowers the developer to use the full capabilities of XML (restrictions, patterns, …) and from there it is fairly easy to generate code for any programming language. One other reason is that the schema in the is the contract between the two systems talking to each other. If the developer creates the WSDL from code, technical dependencies on the specific programming language might be introduced (datatypes, …)
WSDL should be manually composed without using third party tool. Many now adays are not doing so since they find code first approach and generating WSDL is easy for them.
Below is simple code which generates WSDL using @WebService annotation
@WebService
public class TestService {
@WebMethod
public String sayHello(String msg){
return "Hello "+msg;
}
}
Q: What JAXB does?
JAXB helps in converting Java Class to XML and Vice Versa. We always code our business logic in Java but the web service communication always happens by XML. So in order for this to happen we use third party tools like JAXB or JAXB2 plugins.
Q: What is the structure of SOAP Envelope?
A SOAP based web service message is a hierarchical XML packet, consisting of up to four parts.
- Envelope
- Header
- Body(Contains either Request or Response)
- Fault(Found only in Response)
<?xml version = "1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding">
<SOAP-ENV:Header>
...
...
</SOAP-ENV:Header>
<SOAP-ENV:Body>
...
...
<SOAP-ENV:Fault>
...
...
</SOAP-ENV:Fault>
...
</SOAP-ENV:Body>
</SOAP_ENV:Envelope>
Q: What are different part of WSDL Denote?
wsdl:types – Datatypes used in Request and Response. It contains the copy of entire XSD.
wsdl:message – Request and Response structure composed from various datatypes defined above.
wsdl:portType – PortType is like Interface. It tells about methods, their input and output.
wsdl:binding – Binding is like implementation of PortType. Defines how the SOAP service could be accessed over http,https or MQ.
wsdl:service – Contains the port which defines the Endpoint URL, port number
Q: What does REST Webservice follow? Contract First or Service First?
Unlike SOAP that exposes methods and method signatures, REST exposes resources. An understanding of the media types used in the exchange of those resources is all a REST client needs in order to communicate with a REST web service. There is no need for a separate document to describe the resources. Hence REST follows Service First Approach though Contract First Approach could be implemented.
Q: What are the Key Components in Webservices
All the web services has a Service Definition. Service Definition contains the following
- Request and Response Format
- Request Structure
- Response Structure
- API Endpoints
Q: what is the difference between XSD and WSDL?
WSDL (Web Services Description Language) describes your service and its operations – what is the service called, which methods does it offer, what kind of in parameters and return values do these methods have?
It’s a description of the behavior of the service – its functionality.
XSD (XML Schema Definition) describes the static structure of the complex data types being exchanged by those service methods. It describes the types, their fields, any restriction on those fields (like max length or a regex pattern), and so forth.
WSDL documents have an associated XSD that shows what is valid to put in a WSDL document.
Q:Short Descriptions on Tags below
<definitions name = "HelloService"
targetNamespace = "http://www.examples.com/wsdl/HelloService.wsdl"
xmlns:tns = "http://www.examples.com/wsdl/HelloService.wsdl"
xmlns = "http://schemas.xmlsoap.org/wsdl/"
xmlns:soap = "http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd = "http://www.w3.org/2001/XMLSchema">
.
.
</definitions>
- targetNamespace – is a convention of XML Schema that enables the WSDL document to refer to itself
- xmlns:tns – When a complex object type is defined the targetnamespace could be used for deriving the way the object should be interpreted
Refer Here
- xmlns – Unique Identifier for xml elements
- xmlns:soap – It should always have the value of: “http://www.w3.org/2003/05/soap-envelope/”.The namespace defines the Envelope as a SOAP Envelope.
If a different namespace is used, the application generates an error and discards the message.
- xmlns:xsd – XSD is XML Schema Definition Language. XSD files are used to validate that XML files conform to a certain format.Without XML Schema (XSD file) an XML file is a relatively free set of elements and attributes. The XSD file defines which elements and attributes are permitted and in which order. In SOAP, XSD tells how the Structure of Request and Response should be
What is use of xsi:schemalocation?
While developing Webservice XSD is the first file that is created. XSD contains data types that would be used in requests and responses. Once XSD is created Request and Response would be developed based on XSD. xsi:schemalocation is a tag used in response and request which helps in validating the datatype used conforms to one is XSD. In other words, it helps the IDE to keep a check by cross-checking elements defined in request and response are the same as one defined in XSD.
Q1:Difference between concrete wsdl and abstract wsdl
Typically a wsdl contains – types, Messages, porttype, Binding and service
Using Types ,messages and porttype we can’t do anything with that.. which is abstract part. Binding and service has more info about the webservices. So it’s concrete.abstract wsdl is enough to consume a soap service … If we use an abstract wsdl and if we try to generate classes we will not be able to see the java files and only class files can be obtained …
Concrete wsdl can provide complete details about the service…
It is not safe to provide the concrete wsdl to the consumer if he is not to be trusted ..
Please Refer Here
Q2:What is the Difference between SOAP and REST
Refer Here
Q3:What do you mean by Stateless in terms of RESTAPI?
The state of the client application is never stored on the server and is passed on. In this process, the client send all the information that is required for the server to fulfill the HTTP request and the server responds make with HTTP Response.The following are the advantages of Statelessness
HTTP protocol and REST web service, both shares the feature of statelessness. Every method required for communication is identified as an independent method i.e. there are no dependencies to other methods.Any previous communication with the client and server is not maintained and thus the whole process is very much simplified.If any information or metadata used earlier in required in another method, then the client sends again that information with HTTP request.
Q4:What is Resource in RESTApi? What is Resource Representation? Why its important?
‘Resource’ is defined as an object of a type which can be an image, HTML file, text data, and any type of dynamic data.JSON
YAML,XML,HTML are some of the Resource Types.Representation of Resource is important because it determines the easy identification of resources. With proper representations of resource in the proper format, allows the client to easily understand the format.
Q5:What is Caching?Use of Cache Control Header?
Caching is the process in which server response is stored so that a cached copy can be used when required and there is no need of generating the same response again. This process not only reduces the server load but in turn increase the scalability and performance of the server. Only the client is able to cache the response and that too for a limited period of time.
Cache control header can help in attaining cache ability.
Public: Resources that are marked as the public can be cached by any intermediate components(gateways) between the client and server.
Private: Resources that are marked as private can only be cached by the client.
No cache means that particular resource cannot be cached and thus the whole process is stopped.
Q6:What is Payload?
Request data which is present in the body part of every HTTP message is referred as ‘Payload’. In Restful web service, the payload can only be passed to the recipient through POST method.
Q7:Different HTTP methods
GET: This is a read only operation which fetches the list of users on the server.
PUT: This operation is used for the creation of any new resource on the server.
POST: This operation is used for updating an old resource or for creating a new resource.
DELETE: As the name suggests, this operation is used for deleting any resource on the server.
OPTIONS: This operation fetches the list of any supported options of resources that are available on the server.
Q8:What is the difference between PUT method and POST method?
PUT is idempotent, so if you PUT an object twice, it has no effect. You can update or create a resource with PUT with the same object URL.POST is to create new resource.If you are depending on server to create new resource using POST will result in multiple resource creation since server is the one which decides where to place the object. refer here
Q9:What is JAX-RS and what are its implementations?
JAX-RS is defined as the Java API for RESTful web service. Few of its implementations include Jersey,RESTEasy,Apache CFXPlay
Q10:What is use of OPTIONS and HEAD?
OPTIONS tells you things such as “What methods are allowed for this resource”.OPTIONS Identifying which HTTP methods a resource supports, e.g. can we DELETE it or update it via a PUT?OPTIONS method returns info about API (methods/content type)
HTTP/1.1 200 OK
Allow: GET,HEAD,POST,OPTIONS,TRACE
Content-Type: text/html; charset=UTF-8
Date: Wed, 08 May 2013 10:24:43 GMT
Content-Length: 0
HEAD Checking whether a resource has changed. This is useful when maintaining a cached version of a resource.Retrieving metadata about the resource, e.g. its media type or its size, before making a possibly costly retrieval.HEAD gets the HTTP header you would get if you made a GET request, but without the body. This lets the client determine caching information, what content-type would be returned, what status code would be returned.
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Type: text/html; charset=UTF-8
Date: Wed, 08 May 2013 10:12:29 GMT
ETag: "780602-4f6-4db31b2978ec0"
Last-Modified: Thu, 25 Apr 2013 16:13:23 GMT
Content-Length: 1270
Q11.Maximum length of HTTP GET request?
The limit is dependent on both the server and the client.As already mentioned, HTTP itself doesn’t impose any hard-coded limit on request length; but browsers have limits ranging on the 2kb – 8kb .If the limit is exceeded in either the browser or the server, most will just truncate the characters outside the limit without any warning
HTTP 1.1 defines Status Code 414 Request-URI Too Long for the cases where a server-defined limit is reached which is usually configureable somewhere in the server configuration.For the case of client-defined limits, there is no sense on the server returning something, because the server won’t receive the request at all.
Q12.What is need for HttpMessageConverter?
Spring MVC uses HttpMessageConverter to convert the Http request to an object representation and back.Spring Framework then uses one of the Jackson message converters to marshall and unmarshall Java Objects to and from JSON over HTTP.Spring will use the “Accept” header to determine the media type that it needs to respond with and uses the “Content-Type” header to determine the media type of the request body.
Default Message Converters in Spring MVC
StringHttpMessageConverter: it converts Strings from the HTTP request and response.
FormHttpMessageConverter: it converts form data to/from a MultiValueMap.
ByteArrayHttpMessageConverter: it converts byte arrays from the HTTP request and response.
MappingJackson2HttpMessageConverter: it converts JSON from the HTTP request and response.
Jaxb2RootElementHttpMessageConverter: it converts Java objects to/from XML.
SourceHttpMessageConverter: it converts javax.xml.transform.Source from the HTTP request and response.
AtomFeedHttpMessageConverter: it converts Atom feeds.
RssChannelHttpMessageConverter: it converts RSS feeds.
@EnableWebMvc annotation, it automatically registered default Http message converters with application as listed above according to available library in the class path.
Q13.What is WS-Addressing?
WS-Addressing is useful in situations where the SOAP response can’t be served immediately. Either the resources to form the response are not available right away or the result itself takes a long time to be generated. There are times where your business takes time.something which needs manually intervention, something to be approved, whatever, but it takes days to do it. keeping the connection opened all that time and waiting for the response would be inefficient.notification process comes to rescue in these kind of scenarios.The client makes a requests but does not wait for the response. It instead instructs the server where to send the response by use of a “reply to” address. Once the response is available, the server connects to that address and sends the response.Instead of relying on network-level transport to convey routing information, a message utilizing WS-Addressing may contain its own dispatch metadata in a standardized SOAP header.WS-Addressing you are not tied to that. You can demand the response on another type of channel. Request comes on HTTP for example, but you can instruct the server to send the response back over SMTP, for example.
Q14.What is SOAP handler or handler Resolver ?
The handler framework in the Java API for XML Web Services (JAX-WS) allows applications to address cross-cutting and/or system-level concerns by opening the service and client runtimes for applications to plug in modular components. SOAP handler is a SOAP message interceptor, which is able to intercept incoming or outgoing SOAP message and manipulate its values. For example, attach a SOAP handler in client side, which will inject client’s computer MAC address into the SOAP header block for every outgoing SOAP message that is send by the client. In server side, attach another SOAP handler, to retrieve back the client’s MAC address in SOAP header block from every incoming SOAP message.This mechanism also allows the separation of the most fundamental concerns of application software in Web services development, effectively abstracting the system service into handlers and leaving the clients and services to focus on business logic.
JAX-WS specifies two types of handlers:
- Protocol handlers are specific to a protocol and may access or change the protocol-specific aspects of a message
- Logical handlers are protocol-agnostic and act only on the payload of a message
How XML Namespaces work?
Every element (or attribute) in XML belongs to a namespace, a way of “qualifying” the name of the element.Imagine you and I both invent our own XML. You invent XML to describe people, I invent mine to describe cities. Both of us include an element called name. Yours refers to the person’s name, and mine to the city name
<person>
<name>Rob</name>
<age>37</age>
<homecity>
<name>London</name>
<lat>123.000</lat>
<long>0.00</long>
</homecity>
</person>
If our two XMLs were combined into a single document, how would we tell the two names apart? As you can see above, there are two name elements, but they both have different meanings.The answer is that you and I would both assign a namespace to our XML, which we would make unique:
<personxml:person xmlns:personxml="http://www.your.example.com/xml/person"
xmlns:cityxml="http://www.my.example.com/xml/cities">
<personxml:name>Rob</personxml:name>
<personxml:age>37</personxml:age>
<cityxml:homecity>
<cityxml:name>London</cityxml:name>
<cityxml:lat>123.000</cityxml:lat>
<cityxml:long>0.00</cityxml:long>
</cityxml:homecity>
</personxml:person>
All of the tags that start with personxml: are tags belonging to your XML, all the ones that start with cityxml: are mine.
If you declare a namespace without the identifier, that is, xmlns=”http://somenamespace”, rather than xmlns:rob=”somenamespace”, it specifies the default namespace for the document.