Converting jaxbobject to XML
public String jaxbObjToXML(JAXBEntityClassName jaxbObj) throws JAXBException{
JABContext jaxbContext = JAXBContext.newInstaance(JAXBEntityClassName.class);
Marshaller marshaller = jaxbContext .createMarashaller();
//optional
marshaller .setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "schema_URL");
marshaller .setProperty(Marshaller .JAXB_FORMATED_OUTPUT, true);
StringWriter sw = new StringWriter();
marshaller.marshal(jaxbObj, sw);
return sw.toString();
}
Converting jaxbobject to XML using QName when JAXBEntityClassName does not contain root element.
public String jaxbObjToXML(JAXBEntityClassName jaxbObj) throws JAXBException{
JABContext jaxbContext = JAXBContext.newInstaance(JAXBEntityClassName.class);
Marshaller marshaller = jaxbContext .createMarashaller();
marshaller .setProperty(Marshaller .JAXB_FORMATED_OUTPUT, true);
StringWriter sw = new StringWriter();
marshaller.marshal(new JAXBElement<JAXBEntityClassName>(new QName("schema_URL", "JAXBEntityClassName"), JAXBEntityClassName.class, null, jaxbObj) sw);
return sw.toString();
}
Converting XML to JAXB Response object
public JAXBEntityResponseClassName xmlToJAXBObject(String xml) throws JAXBException, XMLStreamException {
XMLInputFactroy xif = XMLInputFactroy.newFactroy();
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
XMLStreamReader xsr = xif.createXMLStreamReader(bis);
JAXBContext jaxbContext = JAXBContext.newInstance("com.xyz.jaxb") //jaxb entities package need to give in double quotes
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<JAXBEntityResponseClassName> jaxbResponseObj
= unmarshaller.unmarshal(xsr, JAXBEntityResponseClassName.class);
return jaxbResponseObj.getValue();
}
Binding Service calling:
public ClientToServiceClass {
private ServiceClass service;
public ClientClass(String wsdlURL, String userName, String password) throws IOException{
QName qname = new QAName("schema_URL", "QNAME_IN_OBJECT_FACTROY");
URL url = new URL(wsdlURL);
HandlerReslover handlerReslover = new ClientUsernameTokenHandler.Reslover(username, password);
Service endpoint = service.creaate(wsdl, qname);
endpoint.setHandlerReslover(handlerReslover);
service = endpoint.getPort(ServiceClass.class);
}
public ResponseClass callServiceWithBindingService(String bindingServiceURL) throws Exception {
BindingProvider bp = null;
bp = (BindingProvider) service;
bp.getRequestContext().put(BindingProvider.ENDPOIT_ADDRESS_PROPERTY, bindingServiceURL);
//Prepare JAXB Request object
JAXBRequest jaxbRequest = new JAXBRequest();
ResponseClass responseObj = service.getServiceMethod(jaxbRequest);
return responseObj;
}
}
wsimport command:
wsimport -p com.abc.xyz -keep -verbose wsdl_URL
public String jaxbObjToXML(JAXBEntityClassName jaxbObj) throws JAXBException{
JABContext jaxbContext = JAXBContext.newInstaance(JAXBEntityClassName.class);
Marshaller marshaller = jaxbContext .createMarashaller();
//optional
marshaller .setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "schema_URL");
marshaller .setProperty(Marshaller .JAXB_FORMATED_OUTPUT, true);
StringWriter sw = new StringWriter();
marshaller.marshal(jaxbObj, sw);
return sw.toString();
}
Converting jaxbobject to XML using QName when JAXBEntityClassName does not contain root element.
public String jaxbObjToXML(JAXBEntityClassName jaxbObj) throws JAXBException{
JABContext jaxbContext = JAXBContext.newInstaance(JAXBEntityClassName.class);
Marshaller marshaller = jaxbContext .createMarashaller();
marshaller .setProperty(Marshaller .JAXB_FORMATED_OUTPUT, true);
StringWriter sw = new StringWriter();
marshaller.marshal(new JAXBElement<JAXBEntityClassName>(new QName("schema_URL", "JAXBEntityClassName"), JAXBEntityClassName.class, null, jaxbObj) sw);
return sw.toString();
}
Converting XML to JAXB Response object
public JAXBEntityResponseClassName xmlToJAXBObject(String xml) throws JAXBException, XMLStreamException {
XMLInputFactroy xif = XMLInputFactroy.newFactroy();
ByteArrayInputStream bis = new ByteArrayInputStream(xml.getBytes());
XMLStreamReader xsr = xif.createXMLStreamReader(bis);
JAXBContext jaxbContext = JAXBContext.newInstance("com.xyz.jaxb") //jaxb entities package need to give in double quotes
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
JAXBElement<JAXBEntityResponseClassName> jaxbResponseObj
= unmarshaller.unmarshal(xsr, JAXBEntityResponseClassName.class);
return jaxbResponseObj.getValue();
}
Binding Service calling:
public ClientToServiceClass {
private ServiceClass service;
public ClientClass(String wsdlURL, String userName, String password) throws IOException{
QName qname = new QAName("schema_URL", "QNAME_IN_OBJECT_FACTROY");
URL url = new URL(wsdlURL);
HandlerReslover handlerReslover = new ClientUsernameTokenHandler.Reslover(username, password);
Service endpoint = service.creaate(wsdl, qname);
endpoint.setHandlerReslover(handlerReslover);
service = endpoint.getPort(ServiceClass.class);
}
public ResponseClass callServiceWithBindingService(String bindingServiceURL) throws Exception {
BindingProvider bp = null;
bp = (BindingProvider) service;
bp.getRequestContext().put(BindingProvider.ENDPOIT_ADDRESS_PROPERTY, bindingServiceURL);
//Prepare JAXB Request object
JAXBRequest jaxbRequest = new JAXBRequest();
ResponseClass responseObj = service.getServiceMethod(jaxbRequest);
return responseObj;
}
}
wsimport command:
wsimport -p com.abc.xyz -keep -verbose wsdl_URL