SOAP web services without JAXB
What if you wanted to develop a SOAP web service without using JAXB? Now, as a Java programmer, why in the world would you not use JAXB? After all, it makes it so easy to map classes to XML representations.
Maybe I am a bit old-fashioned and you have a boss like me, who insists on the good old stuff. Like the closeness to data in XML handling that I enjoy. Automatic transmission is good but manual transmission using shift stick gives you more fine-grained control over your car acceleration / deceleration.
Or maybe just out of curiosity, you would want to find out how this is to be done. ‘This’ meaning writing a SOAP webservice which processes XML files for input and output without using JAXB. In any case, whether you use it or not, it’s good to know how to do it.
Knowing the sequence of the server-side operations of a SOAP call helps understand the steps better. At the end of this post are two extracts from the SOA-Book [1]. The key phase is “Dispatching” where the SOAP request is handed over to the endpoint class. As part of this process, the server calls Handlers before invoking the endpoints. And handlers is where you can try out your XML processing magic.
Since we don’t want to use JAXB, we write our own handler class that extracts the XML from the SOAP request, parses the XML, populates a business object and puts it in the SOAP context. Within the endpoint class, you execute your business logic and use the data you want to send back as StreamSource object.
Let’s get into the details.
The building blocks to begin with. All the required supporting libraries can be taken from Apache CXF, which “is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.”[2]
First step is to create the WSDL file that specifies the request XML and the response XML. The customer (your actual client or your business analyst or dev lead or whoever) has given you the XML files. You take those XML files and make the schema definition (.xsd) files as I have described in a previous blog post, one each for the request and response.
Then you complete the wsdl file as follows:
Here is my sample wsdl:
Next step is to specify your handler chain. This is an xml file, let’s say mySoap_handler.xml, in which you configure your handler name and handler class.
Here is my sample mySoap_handler.xml
We now look at how to write the Handler.
1. Write the handler class, say mySoapHandler, that implements SOAPHandler.
2. Implement the method handleMessage(SOAPMessageContext). In this method --
Here’s my sample handler class
Note that the location of mySoap_handler.xml file is in the same package structure as the mySoapHandler class but in the resources directory of your Maven webapp.
Finally, let’s see how to implement the endpoint class.
1. Your endpoint class, say CustomerEndpoint, has to implement Provider
Maybe I am a bit old-fashioned and you have a boss like me, who insists on the good old stuff. Like the closeness to data in XML handling that I enjoy. Automatic transmission is good but manual transmission using shift stick gives you more fine-grained control over your car acceleration / deceleration.
Or maybe just out of curiosity, you would want to find out how this is to be done. ‘This’ meaning writing a SOAP webservice which processes XML files for input and output without using JAXB. In any case, whether you use it or not, it’s good to know how to do it.
Knowing the sequence of the server-side operations of a SOAP call helps understand the steps better. At the end of this post are two extracts from the SOA-Book [1]. The key phase is “Dispatching” where the SOAP request is handed over to the endpoint class. As part of this process, the server calls Handlers before invoking the endpoints. And handlers is where you can try out your XML processing magic.
Since we don’t want to use JAXB, we write our own handler class that extracts the XML from the SOAP request, parses the XML, populates a business object and puts it in the SOAP context. Within the endpoint class, you execute your business logic and use the data you want to send back as StreamSource object.
Let’s get into the details.
The building blocks to begin with. All the required supporting libraries can be taken from Apache CXF, which “is an open source services framework. CXF helps you build and develop services using frontend programming APIs, like JAX-WS and JAX-RS. These services can speak a variety of protocols such as SOAP, XML/HTTP, RESTful HTTP, or CORBA and work over a variety of transports such as HTTP, JMS or JBI.”[2]
First step is to create the WSDL file that specifies the request XML and the response XML. The customer (your actual client or your business analyst or dev lead or whoever) has given you the XML files. You take those XML files and make the schema definition (.xsd) files as I have described in a previous blog post, one each for the request and response.
Then you complete the wsdl file as follows:
- Import the two namespaces, one for the request and second for the response from the respective xsd files, and specifying their location.
- Define the Request message with parameters element as the root element of your request xsd file.
- Define the Response message with parameters as the root element of your response xsd file.
- Define a portType for operation invoke with input message as tns:Request and output message as tns:Response.
- For the above portType, define a binding.
- Define you service that combines the portType and binding.
Here is my sample wsdl:
Next step is to specify your handler chain. This is an xml file, let’s say mySoap_handler.xml, in which you configure your handler name and handler class.
Here is my sample mySoap_handler.xml
We now look at how to write the Handler.
1. Write the handler class, say mySoapHandler, that implements SOAPHandler
2. Implement the method handleMessage(SOAPMessageContext). In this method --
- Extract SOAP Body and convert to jdom Document.
- Convert w3c document to jdom Document.
- Parse the XML in the Document and build your business object. Use XPath with JDOM2 as I have outlined in my previous post.
- Put the business object into SOAPMessageContext object, say context.
- In context, set the scope of the business object to be MessageContext.Scope.APPLICATION.
- Leave the methods handleFault, close, getHeaders as empty methods.
Here’s my sample handler class
Note that the location of mySoap_handler.xml file is in the same package structure as the mySoapHandler class but in the resources directory of your Maven webapp.
Finally, let’s see how to implement the endpoint class.
1. Your endpoint class, say CustomerEndpoint, has to implement Provider
This comment has been removed by the author.
ReplyDeleteSOAP Web Services India, Soap Web Services Annotations India
ReplyDelete