Skip to main content
Code Review

Return to Question

Notice removed Draw attention by Daniel Newtown
Bounty Ended with Sarfaraz Khan's answer chosen by Daniel Newtown
Tweeted twitter.com/#!/StackCodeReview/status/620497835063156736
Notice added Draw attention by Daniel Newtown
Bounty Started worth 50 reputation by Daniel Newtown
deleted 51 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Need to convert a complete Converting code offor javax.xml.soap.* to webServiceTemplate

I asked following question on StackOverflow but did not receive any answer. Someone suggested me to post it here.

I am able to send requests to the web service using javax.xml.soap.*,. I would like to convert the code to use webServiceTemplate.

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://ticketmaster.productserve.com/v2/soap.php" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
 <soapenv:Header/>
 <soapenv:Body>
 <soap:findEvents soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <request xsi:type="soap:Request">
 <!--You may enter the following 7 items in any order-->
 <apiKey xsi:type="xsd:string">?</apiKey>
 <country xsi:type="xsd:string">?</country>
 <resultsPerPage xsi:type="xsd:int">?</resultsPerPage>
 <currentPage xsi:type="xsd:int">?</currentPage>
 <sort xsi:type="soap:Request_Sort">
 <!--You may enter the following 2 items in any order-->
 <field xsi:type="xsd:string">?</field>
 <order xsi:type="xsd:string">?</order>
 </sort>
 <filters xsi:type="soap:ArrayOfRequest_Filter" soapenc:arrayType="soap:Request_Filter[]"/>
 <updatedSince xsi:type="xsd:string">?</updatedSince>
 </request>
 </soap:findEvents>
 </soapenv:Body>
</soapenv:Envelope>
<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://ticketmaster.productserve.com/v2/soap.php" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
 <soapenv:Header/>
 <soapenv:Body>
 <soap:findEvents soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <request xsi:type="soap:Request">
 <!--You may enter the following 7 items in any order-->
 <apiKey xsi:type="xsd:string">?</apiKey>
 <country xsi:type="xsd:string">?</country>
 <resultsPerPage xsi:type="xsd:int">?</resultsPerPage>
 <currentPage xsi:type="xsd:int">?</currentPage>
 <sort xsi:type="soap:Request_Sort">
 <!--You may enter the following 2 items in any order-->
 <field xsi:type="xsd:string">?</field>
 <order xsi:type="xsd:string">?</order>
 </sort>
 <filters xsi:type="soap:ArrayOfRequest_Filter" soapenc:arrayType="soap:Request_Filter[]"/>
 <updatedSince xsi:type="xsd:string">?</updatedSince>
 </request>
 </soap:findEvents>
 </soapenv:Body>
</soapenv:Envelope>
try {
 SOAPConnectionFactory soapConnectionFactory =
 SOAPConnectionFactory.newInstance();
 SOAPConnection connection =
 soapConnectionFactory.createConnection();
 MessageFactory factory =
 MessageFactory.newInstance();
 SOAPMessage message = factory.createMessage();
 SOAPHeader header = message.getSOAPHeader();
 header.detachNode();
 SOAPBody body = message.getSOAPBody();
 SOAPFactory soapFactory =
 SOAPFactory.newInstance();
 Name bodyName;
 bodyName = soapFactory.createName("findEvents",
 "xsd", "http://ticketmaster.productserve.com/v2/soap.php");
 SOAPBodyElement getList =
 body.addBodyElement(bodyName);
 Name childName = soapFactory.createName("findEvents");
 SOAPElement eventRequest = getList.addChildElement(childName);
 childName = soapFactory.createName("apiKey");
 SOAPElement apiKey = eventRequest.addChildElement(childName);
 apiKey.addTextNode("MYAPI");
 childName = soapFactory.createName("country");
 SOAPElement cid = eventRequest.addChildElement(childName);
 cid.addTextNode("UK");
 message.writeTo(System.out); //show message details
 URL endpoint = new URL("http://ticketmaster.productserve.com/v2/soap.php");
 SOAPMessage response =
 connection.call(message, endpoint);
 connection.close();
 //SOAPBody soapBody = response.getSOAPBody();
 SOAPMessage sm = response;
 System.out.println("Response:");
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 sm.writeTo(out);
 String validSoap = "<?xml version=\"1.0\"?> " + out.toString();
 System.out.println("It is ValidSoap: " + validSoap); //ValidSoap message
 SAXBuilder builder = new SAXBuilder();
 Reader in = new StringReader(validSoap); //reading character stream
 Document doc = null; //empty jDom document is instantiated
 doc = builder.build(in); //build the jDom document
 Element root = doc.getRootElement(); //Envelope
 List allChildren = root.getChildren(); //list of all its child elements
 System.out.println("Root is:" + ((Element) allChildren.get(0)).getName());
 listChildren(root);
} catch (Exception ex) {
 ex.printStackTrace();
}
try {
 SOAPConnectionFactory soapConnectionFactory =
 SOAPConnectionFactory.newInstance();
 SOAPConnection connection =
 soapConnectionFactory.createConnection();
 MessageFactory factory =
 MessageFactory.newInstance();
 SOAPMessage message = factory.createMessage();
 SOAPHeader header = message.getSOAPHeader();
 header.detachNode();
 SOAPBody body = message.getSOAPBody();
 SOAPFactory soapFactory =
 SOAPFactory.newInstance();
 Name bodyName;
 bodyName = soapFactory.createName("findEvents",
 "xsd", "http://ticketmaster.productserve.com/v2/soap.php");
 SOAPBodyElement getList =
 body.addBodyElement(bodyName);
 Name childName = soapFactory.createName("findEvents");
 SOAPElement eventRequest = getList.addChildElement(childName);
 childName = soapFactory.createName("apiKey");
 SOAPElement apiKey = eventRequest.addChildElement(childName);
 apiKey.addTextNode("MYAPI");
 childName = soapFactory.createName("country");
 SOAPElement cid = eventRequest.addChildElement(childName);
 cid.addTextNode("UK");
 message.writeTo(System.out); //show message details
 URL endpoint = new URL("http://ticketmaster.productserve.com/v2/soap.php");
 SOAPMessage response =
 connection.call(message, endpoint);
 connection.close();
 //SOAPBody soapBody = response.getSOAPBody();
 SOAPMessage sm = response;
 System.out.println("Response:");
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 sm.writeTo(out);
 String validSoap = "<?xml version=\"1.0\"?> " + out.toString();
 System.out.println("It is ValidSoap: " + validSoap); //ValidSoap message
 SAXBuilder builder = new SAXBuilder();
 Reader in = new StringReader(validSoap); //reading character stream
 Document doc = null; //empty jDom document is instantiated
 doc = builder.build(in); //build the jDom document
 Element root = doc.getRootElement(); //Envelope
 List allChildren = root.getChildren(); //list of all its child elements
 System.out.println("Root is:" + ((Element) allChildren.get(0)).getName());
 listChildren(root);
} catch (Exception ex) {
 ex.printStackTrace();
}
 webServiceTemplate.sendSourceAndReceiveToResult
 ("http://ticketmaster.productserve.com/v2/soap.php",source, result);
@XmlRootElement
public class FindEvents {
 @XmlElement
 Request request;
 public Request getRequest() {
 return request;
 }
 public void setRequest(Request request) {
 this.request = request;
 }
}
@XmlSeeAlso(SortTicket.class)
public class Request {
 @XmlElement
 String apiKey;
 @XmlElement
 String country;
 @XmlElement
 int resultsPerPage;
 @XmlElement
 int currentPage;
 @XmlElement(name = "Sort")
 SortTicket sort;
 @XmlElement
 String[] filters;
 @XmlElement
 String updatedSince;
 public String getApiKey() {
 return apiKey;
 }
 public void setApiKey(String apiKey) {
 this.apiKey = apiKey;
 }
 public String getCountry() {
 return country;
 }
 public void setCountry(String country) {
 this.country = country;
 }
 public int getResultsPerPage() {
 return resultsPerPage;
 }
 public void setResultsPerPage(int resultsPerPage) {
 this.resultsPerPage = resultsPerPage;
 }
 public int getCurrentPage() {
 return currentPage;
 }
 public void setCurrentPage(int currentPage) {
 this.currentPage = currentPage;
 }
 public SortTicket getSort() {
 return sort;
 }
 public void setSort(SortTicket sort) {
 this.sort = sort;
 }
 public String[] getFilters() {
 return filters;
 }
 public void setFilters(String[] filters) {
 this.filters = filters;
 }
 public String getUpdatedSince() {
 return updatedSince;
 }
 public void setUpdatedSince(String updatedSince) {
 this.updatedSince = updatedSince;
 }
}
public class SortTicket {
 @XmlElement
 String field;
 @XmlElement
 String order;
 public String getField() {
 return field;
 }
 public void setField(String field) {
 this.field = field;
 }
 public String getOrder() {
 return order;
 }
 public void setOrder(String order) {
 this.order = order;
 }
}
 webServiceTemplate.sendSourceAndReceiveToResult
 ("http://ticketmaster.productserve.com/v2/soap.php",source, result);
@XmlRootElement
public class FindEvents {
 @XmlElement
 Request request;
 public Request getRequest() {
 return request;
 }
 public void setRequest(Request request) {
 this.request = request;
 }
}
@XmlSeeAlso(SortTicket.class)
public class Request {
 @XmlElement
 String apiKey;
 @XmlElement
 String country;
 @XmlElement
 int resultsPerPage;
 @XmlElement
 int currentPage;
 @XmlElement(name = "Sort")
 SortTicket sort;
 @XmlElement
 String[] filters;
 @XmlElement
 String updatedSince;
 public String getApiKey() {
 return apiKey;
 }
 public void setApiKey(String apiKey) {
 this.apiKey = apiKey;
 }
 public String getCountry() {
 return country;
 }
 public void setCountry(String country) {
 this.country = country;
 }
 public int getResultsPerPage() {
 return resultsPerPage;
 }
 public void setResultsPerPage(int resultsPerPage) {
 this.resultsPerPage = resultsPerPage;
 }
 public int getCurrentPage() {
 return currentPage;
 }
 public void setCurrentPage(int currentPage) {
 this.currentPage = currentPage;
 }
 public SortTicket getSort() {
 return sort;
 }
 public void setSort(SortTicket sort) {
 this.sort = sort;
 }
 public String[] getFilters() {
 return filters;
 }
 public void setFilters(String[] filters) {
 this.filters = filters;
 }
 public String getUpdatedSince() {
 return updatedSince;
 }
 public void setUpdatedSince(String updatedSince) {
 this.updatedSince = updatedSince;
 }
}
public class SortTicket {
 @XmlElement
 String field;
 @XmlElement
 String order;
 public String getField() {
 return field;
 }
 public void setField(String field) {
 this.field = field;
 }
 public String getOrder() {
 return order;
 }
 public void setOrder(String order) {
 this.order = order;
 }
}

Need to convert a complete code of javax.xml.soap.* to webServiceTemplate

I asked following question on StackOverflow but did not receive any answer. Someone suggested me to post it here.

I am able to send requests to the web service using javax.xml.soap.*, I would like to convert the code to use webServiceTemplate.

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://ticketmaster.productserve.com/v2/soap.php" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
 <soapenv:Header/>
 <soapenv:Body>
 <soap:findEvents soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <request xsi:type="soap:Request">
 <!--You may enter the following 7 items in any order-->
 <apiKey xsi:type="xsd:string">?</apiKey>
 <country xsi:type="xsd:string">?</country>
 <resultsPerPage xsi:type="xsd:int">?</resultsPerPage>
 <currentPage xsi:type="xsd:int">?</currentPage>
 <sort xsi:type="soap:Request_Sort">
 <!--You may enter the following 2 items in any order-->
 <field xsi:type="xsd:string">?</field>
 <order xsi:type="xsd:string">?</order>
 </sort>
 <filters xsi:type="soap:ArrayOfRequest_Filter" soapenc:arrayType="soap:Request_Filter[]"/>
 <updatedSince xsi:type="xsd:string">?</updatedSince>
 </request>
 </soap:findEvents>
 </soapenv:Body>
</soapenv:Envelope>
try {
 SOAPConnectionFactory soapConnectionFactory =
 SOAPConnectionFactory.newInstance();
 SOAPConnection connection =
 soapConnectionFactory.createConnection();
 MessageFactory factory =
 MessageFactory.newInstance();
 SOAPMessage message = factory.createMessage();
 SOAPHeader header = message.getSOAPHeader();
 header.detachNode();
 SOAPBody body = message.getSOAPBody();
 SOAPFactory soapFactory =
 SOAPFactory.newInstance();
 Name bodyName;
 bodyName = soapFactory.createName("findEvents",
 "xsd", "http://ticketmaster.productserve.com/v2/soap.php");
 SOAPBodyElement getList =
 body.addBodyElement(bodyName);
 Name childName = soapFactory.createName("findEvents");
 SOAPElement eventRequest = getList.addChildElement(childName);
 childName = soapFactory.createName("apiKey");
 SOAPElement apiKey = eventRequest.addChildElement(childName);
 apiKey.addTextNode("MYAPI");
 childName = soapFactory.createName("country");
 SOAPElement cid = eventRequest.addChildElement(childName);
 cid.addTextNode("UK");
 message.writeTo(System.out); //show message details
 URL endpoint = new URL("http://ticketmaster.productserve.com/v2/soap.php");
 SOAPMessage response =
 connection.call(message, endpoint);
 connection.close();
 //SOAPBody soapBody = response.getSOAPBody();
 SOAPMessage sm = response;
 System.out.println("Response:");
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 sm.writeTo(out);
 String validSoap = "<?xml version=\"1.0\"?> " + out.toString();
 System.out.println("It is ValidSoap: " + validSoap); //ValidSoap message
 SAXBuilder builder = new SAXBuilder();
 Reader in = new StringReader(validSoap); //reading character stream
 Document doc = null; //empty jDom document is instantiated
 doc = builder.build(in); //build the jDom document
 Element root = doc.getRootElement(); //Envelope
 List allChildren = root.getChildren(); //list of all its child elements
 System.out.println("Root is:" + ((Element) allChildren.get(0)).getName());
 listChildren(root);
} catch (Exception ex) {
 ex.printStackTrace();
}
 webServiceTemplate.sendSourceAndReceiveToResult
 ("http://ticketmaster.productserve.com/v2/soap.php",source, result);
@XmlRootElement
public class FindEvents {
 @XmlElement
 Request request;
 public Request getRequest() {
 return request;
 }
 public void setRequest(Request request) {
 this.request = request;
 }
}
@XmlSeeAlso(SortTicket.class)
public class Request {
 @XmlElement
 String apiKey;
 @XmlElement
 String country;
 @XmlElement
 int resultsPerPage;
 @XmlElement
 int currentPage;
 @XmlElement(name = "Sort")
 SortTicket sort;
 @XmlElement
 String[] filters;
 @XmlElement
 String updatedSince;
 public String getApiKey() {
 return apiKey;
 }
 public void setApiKey(String apiKey) {
 this.apiKey = apiKey;
 }
 public String getCountry() {
 return country;
 }
 public void setCountry(String country) {
 this.country = country;
 }
 public int getResultsPerPage() {
 return resultsPerPage;
 }
 public void setResultsPerPage(int resultsPerPage) {
 this.resultsPerPage = resultsPerPage;
 }
 public int getCurrentPage() {
 return currentPage;
 }
 public void setCurrentPage(int currentPage) {
 this.currentPage = currentPage;
 }
 public SortTicket getSort() {
 return sort;
 }
 public void setSort(SortTicket sort) {
 this.sort = sort;
 }
 public String[] getFilters() {
 return filters;
 }
 public void setFilters(String[] filters) {
 this.filters = filters;
 }
 public String getUpdatedSince() {
 return updatedSince;
 }
 public void setUpdatedSince(String updatedSince) {
 this.updatedSince = updatedSince;
 }
}
public class SortTicket {
 @XmlElement
 String field;
 @XmlElement
 String order;
 public String getField() {
 return field;
 }
 public void setField(String field) {
 this.field = field;
 }
 public String getOrder() {
 return order;
 }
 public void setOrder(String order) {
 this.order = order;
 }
}

Converting code for javax.xml.soap.* to webServiceTemplate

I am able to send requests to the web service using javax.xml.soap.*. I would like to convert the code to use webServiceTemplate.

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://ticketmaster.productserve.com/v2/soap.php" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
 <soapenv:Header/>
 <soapenv:Body>
 <soap:findEvents soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <request xsi:type="soap:Request">
 <!--You may enter the following 7 items in any order-->
 <apiKey xsi:type="xsd:string">?</apiKey>
 <country xsi:type="xsd:string">?</country>
 <resultsPerPage xsi:type="xsd:int">?</resultsPerPage>
 <currentPage xsi:type="xsd:int">?</currentPage>
 <sort xsi:type="soap:Request_Sort">
 <!--You may enter the following 2 items in any order-->
 <field xsi:type="xsd:string">?</field>
 <order xsi:type="xsd:string">?</order>
 </sort>
 <filters xsi:type="soap:ArrayOfRequest_Filter" soapenc:arrayType="soap:Request_Filter[]"/>
 <updatedSince xsi:type="xsd:string">?</updatedSince>
 </request>
 </soap:findEvents>
 </soapenv:Body>
</soapenv:Envelope>
try {
 SOAPConnectionFactory soapConnectionFactory =
 SOAPConnectionFactory.newInstance();
 SOAPConnection connection =
 soapConnectionFactory.createConnection();
 MessageFactory factory =
 MessageFactory.newInstance();
 SOAPMessage message = factory.createMessage();
 SOAPHeader header = message.getSOAPHeader();
 header.detachNode();
 SOAPBody body = message.getSOAPBody();
 SOAPFactory soapFactory =
 SOAPFactory.newInstance();
 Name bodyName;
 bodyName = soapFactory.createName("findEvents",
 "xsd", "http://ticketmaster.productserve.com/v2/soap.php");
 SOAPBodyElement getList =
 body.addBodyElement(bodyName);
 Name childName = soapFactory.createName("findEvents");
 SOAPElement eventRequest = getList.addChildElement(childName);
 childName = soapFactory.createName("apiKey");
 SOAPElement apiKey = eventRequest.addChildElement(childName);
 apiKey.addTextNode("MYAPI");
 childName = soapFactory.createName("country");
 SOAPElement cid = eventRequest.addChildElement(childName);
 cid.addTextNode("UK");
 message.writeTo(System.out); //show message details
 URL endpoint = new URL("http://ticketmaster.productserve.com/v2/soap.php");
 SOAPMessage response =
 connection.call(message, endpoint);
 connection.close();
 //SOAPBody soapBody = response.getSOAPBody();
 SOAPMessage sm = response;
 System.out.println("Response:");
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 sm.writeTo(out);
 String validSoap = "<?xml version=\"1.0\"?> " + out.toString();
 System.out.println("It is ValidSoap: " + validSoap); //ValidSoap message
 SAXBuilder builder = new SAXBuilder();
 Reader in = new StringReader(validSoap); //reading character stream
 Document doc = null; //empty jDom document is instantiated
 doc = builder.build(in); //build the jDom document
 Element root = doc.getRootElement(); //Envelope
 List allChildren = root.getChildren(); //list of all its child elements
 System.out.println("Root is:" + ((Element) allChildren.get(0)).getName());
 listChildren(root);
} catch (Exception ex) {
 ex.printStackTrace();
}
 webServiceTemplate.sendSourceAndReceiveToResult
 ("http://ticketmaster.productserve.com/v2/soap.php",source, result);
@XmlRootElement
public class FindEvents {
 @XmlElement
 Request request;
 public Request getRequest() {
 return request;
 }
 public void setRequest(Request request) {
 this.request = request;
 }
}
@XmlSeeAlso(SortTicket.class)
public class Request {
 @XmlElement
 String apiKey;
 @XmlElement
 String country;
 @XmlElement
 int resultsPerPage;
 @XmlElement
 int currentPage;
 @XmlElement(name = "Sort")
 SortTicket sort;
 @XmlElement
 String[] filters;
 @XmlElement
 String updatedSince;
 public String getApiKey() {
 return apiKey;
 }
 public void setApiKey(String apiKey) {
 this.apiKey = apiKey;
 }
 public String getCountry() {
 return country;
 }
 public void setCountry(String country) {
 this.country = country;
 }
 public int getResultsPerPage() {
 return resultsPerPage;
 }
 public void setResultsPerPage(int resultsPerPage) {
 this.resultsPerPage = resultsPerPage;
 }
 public int getCurrentPage() {
 return currentPage;
 }
 public void setCurrentPage(int currentPage) {
 this.currentPage = currentPage;
 }
 public SortTicket getSort() {
 return sort;
 }
 public void setSort(SortTicket sort) {
 this.sort = sort;
 }
 public String[] getFilters() {
 return filters;
 }
 public void setFilters(String[] filters) {
 this.filters = filters;
 }
 public String getUpdatedSince() {
 return updatedSince;
 }
 public void setUpdatedSince(String updatedSince) {
 this.updatedSince = updatedSince;
 }
}
public class SortTicket {
 @XmlElement
 String field;
 @XmlElement
 String order;
 public String getField() {
 return field;
 }
 public void setField(String field) {
 this.field = field;
 }
 public String getOrder() {
 return order;
 }
 public void setOrder(String order) {
 this.order = order;
 }
}
Post Reopened by maaartinus, Abbas, Quill, Mast , Morwenn
Fix some slight things in the grammar.
Source Link
Morwenn
  • 20.2k
  • 3
  • 69
  • 132

I am able to send requests to the web service using javax.xml.soap.*, I would like to covertconvert the code to use webServiceTemplate.

  • I am struggling with creating request and result objects. (sample IveI've found is related to xml not SOAP)
  • I am also wondering ifwhether there is anyare advantages ofto using webServiceTemplate overover java.xml.soap. If there is not, am I doing it correctly? Given that I need to get connected to 20 web services.

I am able to send requests to the web service using javax.xml.soap.*, I would like to covert the code to use webServiceTemplate.

  • I am struggling with creating request and result objects. (sample Ive found is related to xml not SOAP)
  • I am also wondering if there is any advantages of using webServiceTemplate over java.xml.soap. If there is not am I doing it correctly? Given that I need to get connected to 20 web services.

I am able to send requests to the web service using javax.xml.soap.*, I would like to convert the code to use webServiceTemplate.

  • I am struggling with creating request and result objects. (sample I've found is related to xml not SOAP)
  • I am also wondering whether there are advantages to using webServiceTemplate over java.xml.soap. If there is not, am I doing it correctly? Given that I need to get connected to 20 web services.
Post Closed as "Not suitable for this site" by Mathieu Guindon, Community Bot, Jamal
Source Link

Need to convert a complete code of javax.xml.soap.* to webServiceTemplate

I asked following question on StackOverflow but did not receive any answer. Someone suggested me to post it here.

I am able to send requests to the web service using javax.xml.soap.*, I would like to covert the code to use webServiceTemplate.

  • I am struggling with creating request and result objects. (sample Ive found is related to xml not SOAP)
  • I am also wondering if there is any advantages of using webServiceTemplate over java.xml.soap. If there is not am I doing it correctly? Given that I need to get connected to 20 web services.

The only service it has is findEvents as follows:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soap="http://ticketmaster.productserve.com/v2/soap.php" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
 <soapenv:Header/>
 <soapenv:Body>
 <soap:findEvents soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
 <request xsi:type="soap:Request">
 <!--You may enter the following 7 items in any order-->
 <apiKey xsi:type="xsd:string">?</apiKey>
 <country xsi:type="xsd:string">?</country>
 <resultsPerPage xsi:type="xsd:int">?</resultsPerPage>
 <currentPage xsi:type="xsd:int">?</currentPage>
 <sort xsi:type="soap:Request_Sort">
 <!--You may enter the following 2 items in any order-->
 <field xsi:type="xsd:string">?</field>
 <order xsi:type="xsd:string">?</order>
 </sort>
 <filters xsi:type="soap:ArrayOfRequest_Filter" soapenc:arrayType="soap:Request_Filter[]"/>
 <updatedSince xsi:type="xsd:string">?</updatedSince>
 </request>
 </soap:findEvents>
 </soapenv:Body>
</soapenv:Envelope>

My code is as follows:

try {
 SOAPConnectionFactory soapConnectionFactory =
 SOAPConnectionFactory.newInstance();
 SOAPConnection connection =
 soapConnectionFactory.createConnection();
 MessageFactory factory =
 MessageFactory.newInstance();
 SOAPMessage message = factory.createMessage();
 SOAPHeader header = message.getSOAPHeader();
 header.detachNode();
 SOAPBody body = message.getSOAPBody();
 SOAPFactory soapFactory =
 SOAPFactory.newInstance();
 Name bodyName;
 bodyName = soapFactory.createName("findEvents",
 "xsd", "http://ticketmaster.productserve.com/v2/soap.php");
 SOAPBodyElement getList =
 body.addBodyElement(bodyName);
 Name childName = soapFactory.createName("findEvents");
 SOAPElement eventRequest = getList.addChildElement(childName);
 childName = soapFactory.createName("apiKey");
 SOAPElement apiKey = eventRequest.addChildElement(childName);
 apiKey.addTextNode("MYAPI");
 childName = soapFactory.createName("country");
 SOAPElement cid = eventRequest.addChildElement(childName);
 cid.addTextNode("UK");
 message.writeTo(System.out); //show message details
 URL endpoint = new URL("http://ticketmaster.productserve.com/v2/soap.php");
 SOAPMessage response =
 connection.call(message, endpoint);
 connection.close();
 //SOAPBody soapBody = response.getSOAPBody();
 SOAPMessage sm = response;
 System.out.println("Response:");
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 sm.writeTo(out);
 String validSoap = "<?xml version=\"1.0\"?> " + out.toString();
 System.out.println("It is ValidSoap: " + validSoap); //ValidSoap message
 SAXBuilder builder = new SAXBuilder();
 Reader in = new StringReader(validSoap); //reading character stream
 Document doc = null; //empty jDom document is instantiated
 doc = builder.build(in); //build the jDom document
 Element root = doc.getRootElement(); //Envelope
 List allChildren = root.getChildren(); //list of all its child elements
 System.out.println("Root is:" + ((Element) allChildren.get(0)).getName());
 listChildren(root);
} catch (Exception ex) {
 ex.printStackTrace();
}

New Code

 webServiceTemplate.sendSourceAndReceiveToResult
 ("http://ticketmaster.productserve.com/v2/soap.php",source, result);
@XmlRootElement
public class FindEvents {
 @XmlElement
 Request request;
 public Request getRequest() {
 return request;
 }
 public void setRequest(Request request) {
 this.request = request;
 }
}
@XmlSeeAlso(SortTicket.class)
public class Request {
 @XmlElement
 String apiKey;
 @XmlElement
 String country;
 @XmlElement
 int resultsPerPage;
 @XmlElement
 int currentPage;
 @XmlElement(name = "Sort")
 SortTicket sort;
 @XmlElement
 String[] filters;
 @XmlElement
 String updatedSince;
 public String getApiKey() {
 return apiKey;
 }
 public void setApiKey(String apiKey) {
 this.apiKey = apiKey;
 }
 public String getCountry() {
 return country;
 }
 public void setCountry(String country) {
 this.country = country;
 }
 public int getResultsPerPage() {
 return resultsPerPage;
 }
 public void setResultsPerPage(int resultsPerPage) {
 this.resultsPerPage = resultsPerPage;
 }
 public int getCurrentPage() {
 return currentPage;
 }
 public void setCurrentPage(int currentPage) {
 this.currentPage = currentPage;
 }
 public SortTicket getSort() {
 return sort;
 }
 public void setSort(SortTicket sort) {
 this.sort = sort;
 }
 public String[] getFilters() {
 return filters;
 }
 public void setFilters(String[] filters) {
 this.filters = filters;
 }
 public String getUpdatedSince() {
 return updatedSince;
 }
 public void setUpdatedSince(String updatedSince) {
 this.updatedSince = updatedSince;
 }
}
public class SortTicket {
 @XmlElement
 String field;
 @XmlElement
 String order;
 public String getField() {
 return field;
 }
 public void setField(String field) {
 this.field = field;
 }
 public String getOrder() {
 return order;
 }
 public void setOrder(String order) {
 this.order = order;
 }
}
lang-java

AltStyle によって変換されたページ (->オリジナル) /