HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaspringMinor

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

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javaxwebservicetemplatexmlsoapforcodeconverting

Problem

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.

The only service it has is findEvents as follows:







?
?
?
?


?
?


?






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.produ

Solution

Since you have generated DTO classes with Jaxb annotation you can create a marshaller ,unmarshaller and create objects of the DTO classes (SortTicket, Request, FindEvents) and send the objects directly instead of using the xml request

webServiceTemplate.marshalSendAndReceive(findEvents);


Something like this you'll have to configure.

Create a marshaller



create web service template


    
    
    


and in some class's method where you want to send soap request inject webServiceTemplate using @Autowired

@Autowired
private WebServiceTemplate webServiceTemplate;

public void sendSampleSoapRequest() {

   SortTicket sortTicket=new SortTicket();
   // set its values
   Request request=new Request();
   //set its values
   request.setSort(sortTicket);
   FindEvents findEvents=new FindEvents();
   setRequest(request)
   Object response=webServiceTemplate.marshalSendAndReceive(findEvents);
 }


marshalSendAndReceive message uses the Jaxb marshaller to convert your objects (marked with JaxB annotation)to xml.So above your findEvents object will be converted to its xml from. Advantage of using this is that you will be getting rid of creating xml elements manually.

Regarding your second point
advantages of using webServiceTemplate over java.xml.soap. : you don't have to create those SOAPElements manually you just create an object and send it instead of big code for manually handling it.
Since you'll have to connect to 20 different webservices it will be much easier for you to create DTO objects and send them directly.You may need to modify my above samples a little. Remove the deault uri


    
    


and while sending request give the URI request

Object response=webServiceTemplate.marshalSendAndReceive(uri,object);


For sending it to multiple server

Object response1=webServiceTemplate.marshalSendAndReceive(uri1,object);
Object response1=webServiceTemplate.marshalSendAndReceive(uri2,object)


uri1 and uri2 can be different soap service and if you don't have the wsdl you can send xml with this method

sendSourceAndReceiveToResult(uri1,source, result);
sendSourceAndReceiveToResult(uri2,source, result);


Sending a uri in the send method over rides the default URI

For example check this also check the api doc

Code Snippets

webServiceTemplate.marshalSendAndReceive(findEvents);
<oxm:jaxb2-marshaller id="marshaller" contextPath="com.yourcontextpath" />
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    <property name="marshaller" ref="marshaller" />
    <property name="unmarshaller" ref="marshaller" />
    <property name="defaultUri"
        value="http://ticketmaster.productserve.com/v2/soap.php" />
</bean>
@Autowired
private WebServiceTemplate webServiceTemplate;

public void sendSampleSoapRequest() {

   SortTicket sortTicket=new SortTicket();
   // set its values
   Request request=new Request();
   //set its values
   request.setSort(sortTicket);
   FindEvents findEvents=new FindEvents();
   setRequest(request)
   Object response=webServiceTemplate.marshalSendAndReceive(findEvents);
 }
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
    <property name="marshaller" ref="marshaller" />
    <property name="unmarshaller" ref="marshaller" />
</bean>

Context

StackExchange Code Review Q#95633, answer score: 2

Revisions (0)

No revisions yet.