Building WebService with Custom Serializer using JDeveloper
JDeveloper 10g comes with UI based web service assembler. It converts simple java class to web service. You can customize web service assembling using Custom Serializer in case Service class accepts pojo class as parameters or return it. This post explains creating web service from simple java class. It is considered that you have gone through previous posts – JAXB by Jdeveloper and XML Document by Jdeveloper. So you have xml schema , xml document and set of java classes generated by JAXB compilation
Here is step by step approach
1. Code a CustomSerializer , it has to implement SOAPElementSerializer and override deserialize and serialize methods. Sample code is as below
package jaxb.serializer;
import javax.xml.bind.JAXBException;
import oracle.webservices.databinding.SOAPElementSerializer;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.Text;
import javax.xml.namespace.QName;
import java.util.Iterator;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import jaxb.types.ObjectFactory;
import jaxb.types.OrderImpl;
public class CustomSerializer implements SOAPElementSerializer {
private Map initParams;
protected SOAPFactory soapFactory;
public CustomSerializer() {
}
public void init(Map prop) {
initParams = prop;
try {
soapFactory = SOAPFactory.newInstance();
} catch (SOAPException e) {
e.printStackTrace();
}
}
public Object deserialize(SOAPElement ele) {
System.out.println("Inside deserialize");
String str = "";
OrderImpl order = null;
System.out.println(ele.getNodeName());
System.out.println("Input Element for deserialize " + ele.toString());
JAXBContext context;
try {
context = JAXBContext.newInstance("jaxb.types");
Unmarshaller um = context.createUnmarshaller();
order = (OrderImpl) um.unmarshal(ele);
System.out.println("deserialize : XML to Object is done");
System.out.println(order.getOrderId());
System.out.println(order.getOrderDesc());
} catch (JAXBException e) {
e.printStackTrace();
// TODO
}
return order;
}
/**
* The qname parameter must be used to create the top level element.
*/
public SOAPElement serialize(QName qname, Object obj) {
System.out.println("Inside serialize");
SOAPElement xml=null;
try {
xml = soapFactory.createElement(qname.getLocalPart(), qname.getPrefix(),
qname.getNamespaceURI());
JAXBContext context = JAXBContext.newInstance("jaxb.types");
Marshaller m = context.createMarshaller();
m.marshal(obj, xml);
} catch (JAXBException e) {
e.printStackTrace();
} catch (SOAPException e) {
e.printStackTrace();
// TODO
}
return xml;
}
}
2. Code simple service class
package jaxb.service;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import jaxb.types.Order;
import jaxb.types.OrderImpl;
public class OrderDetails {
public OrderDetails() {
}
public OrderImpl getOrderDetails(int orderId){
JAXBContext context;
OrderImpl order = null;
try {
context = JAXBContext.newInstance("jaxb.types");
//Unmarshalling
Unmarshaller um = context.createUnmarshaller();
order = (OrderImpl) um.unmarshal(new File("D:\\jdev10134\\jdev\\mywork\\JAXBApplication\\JAXBProject\\sample_order.xml"));
System.out.println("Unmarshalling XML -> Java");
System.out.println("Order Id =" + order.getOrderId());
System.out.println("Order Desc = "+order.getOrderDesc());
} catch (JAXBException e) {
e.printStackTrace();
}
return order;
}
}
3. Right click this service class select “Create J2EE WebService”
4. Give Web Service name and follow all default setting of the Wizard except step 3
3. In Step 3 Wizard provides option of declaring custom mapping between java class and xml element and corresponding Serializer.
4. End of this Wizard , our Web Service will be ready . Right click the Service and RUN it . You will See URL for the same web service. Just open the URL in browser , you will get web service Test page with “Invoke” option.
5. Output will be
Hi Yogesh,
ReplyDeleteGreat BLOG post!
I was struggling with this subject for a long time....
At step 3 to map the custom XML data type serializer my JDeveloper drop down to select XML qualified name was always empty.
Somehow under the hood there is some reference to a class or configuration file.
For this example, I want to extract the item details from the database, how can I do? Please give me an example by using this..thanks
ReplyDelete