JAXBBindingExample.java
Note: The example uses a JAXB binding and other classes. The topic code sample does not show these. Please refer to the zipped project for those details.
package jaxbbindingexample; import java.io.IOException; import java.io.StringReader; import java.io.StringWriter; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; public class JAXBBindingExample { public static void main(String[] args) throws IOException, JAXBException { //Create a request object AddressTypeCodeService.GetTableEntryListRequest listRequestObj = new AddressTypeCodeService.GetTableEntryListRequest(); AddressTypeCodeService.ObjectFactory objectFactoryAddTyCoSvc = new AddressTypeCodeService.ObjectFactory(); listRequestObj = objectFactoryAddTyCoSvc.createGetTableEntryListRequest(); //JAXBContext for the request JAXBContext contextForRequest = JAXBContext.newInstance (AddressTypeCodeService.GetTableEntryListRequest.class); //Marshaller for the request Marshaller m = contextForRequest.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); //Writer for the request StringWriter st = new StringWriter(); m.marshal(listRequestObj, st); String listRequestStr = st.toString(); //Wrap the request message with SOAP elements and XML declaration. listRequestStr = MessageAdjustments.fixSOAPRequestMessage(listRequestStr); //String for the endpoint //Don't forget to adjust for the square brackets. String urlString = "http://localhost/bbAppFx/vpp/bizops/db%5BBBINFINITY%5D/codetables/ADDRESSTYPECODE/soap.asmx"; //This will send the message and place the SOAP reply message in a //String. String listReplyStr = HttpURLConnectionTechnique.createHttpURLConnectionAndMakeRequest (listRequestStr, urlString); //Remove the SOAP wrapping and XML declaration from the reply message. listReplyStr = MessageAdjustments.stripSOAPReplyMessage(listReplyStr); //Create a reply object. AddressTypeCodeService.GetTableEntryListReply listReplyObj = new AddressTypeCodeService.GetTableEntryListReply(); listReplyObj = objectFactoryAddTyCoSvc.createGetTableEntryListReply(); //JAXBContext for the the reply JAXBContext contextForReply = JAXBContext.newInstance (AddressTypeCodeService.GetTableEntryListReply.class); //Unmarshaller for the reply Unmarshaller u = contextForReply.createUnmarshaller(); StringBuilder xmlStr = new StringBuilder(listReplyStr); //Unmarshal the reply string into the Java object for the reply. listReplyObj = (AddressTypeCodeService.GetTableEntryListReply) u.unmarshal (new StreamSource( new StringReader(xmlStr.toString()))); //Output window System.out.println(listRequestStr); System.out.println("\nReply message:\n"); System.out.println(listReplyStr); System.out.println("\nValue of reply message object:\n"); System.out.println (listReplyObj.getRows().getTableEntryListRow().get(0).getID()); } }