Help with simple SpringWS SOAP Client (after GroovyWS/soapUI brain rot)
I'm trying to use SpringWS in order to have a Java client access the services at http://www.webservicex.com/stockquote.asmx?WSDL (this is just an example service I'm trying to use to get me started).
It's been a while since I wrote a SOAP client (using Axis & wsdl2java), so I'm a bit fuzzy on how this should work. Not to mention, I've most recently looked at GroovyWS, which lets you do things like:
Code:
proxy = new WSClient("http://www.w3schools.com/webservices/tempconvert.asmx?WSDL", this.class.classLoader)
proxy.initialize()
result = proxy.CelsiusToFahrenheit(0)
println "You are probably freezing at ${result} degrees Farhenheit"
Also, have played with soapUI, which sort of lets you do similar things (i.e. not have to do any marshalling/unmarshalling, build classes based of the .xsd's in the wsdl, etc).
Given that, what I thought I could do was create a class like so:
Code:
class WebServiceClient {
private static Logger LOG = Logger.getLogger(WebServiceClient.class);
private static final String MESSAGE = """..."""
private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate();
public void setDefaultUri(String defaultUri) {
webServiceTemplate.setDefaultUri(defaultUri);
}
// send to the configured default URI
public boolean simpleSendAndReceive() {
LOG.debug "simple send and recieve"
StreamSource source = new StreamSource(new StringReader(newMessage));
StreamResult result = new StreamResult(System.out);
webServiceTemplate.sendSourceAndReceiveToResult(source, result);
LOG.debug("result was: ${result.getClass()}")
LOG.error("sent using simpleSendAndReceive")
true
}
}
with a spring file of:
Code:
<bean id="webServiceTemplate12" class="org.springframework.ws.client.core.WebServiceTemplate" autowire="byType" autowire-candidate="false">
</bean>
<bean id="webServiceClient" class="gov.oss.fist.springws.test.util.WebServiceClient">
<property name="defaultUri" value="http://www.webservicex.com/stockquote.asmx"/>
</bean>
</beans>
and then call webServiceClient.simpleSendAndReceive(), i.e.
Code:
assert wsc.simpleSendAndReceive() == true : "uh oh soap call failed"
It makes the SOAP call okay, but I get an error about the HTTP header SoapAction being invalid/missing. (If I was going to debug based on HTTP headers, I should be using REST...but I digress ;) ).
I then tried something like this (based on the raw message/request in soapUI):
Code:
webServiceTemplate.sendSourceAndReceiveToResult(source, new SoapActionCallback("http://www.webserviceX.NET/GetQuote"), result)
...
public void marshallWithSoapActionHeader(def o) {
webServiceTemplate.marshalSendAndReceive(o, new WebServiceMessageCallback() {
public void doWithMessage(WebServiceMessage message) {
((SoapMessage)message).setSoapAction("http://www.webserviceX.NET/GetQuote");
LOG.error("message: ${message}")
}
});
}
but got an "exception" from that service. So again, the SOAP message is being sent at least somewhat correctly and this time I actually get a SOAP response.
FWIW, the service works fine in soapUI.
What I'm wondering - more so than the specific errors - is if I'm even on the right path.
Do I have to use mvn/jaxb to build beans based on the .xsd's in the schema? Or does SpringWS/WebServiceTemplate do something like GroovyWS and create those beans after it gets injected with the .wsdl URI?
Do I need to specifically define xml marshaller/unmarshallers?
Does anyone have some sample code that just acts as a SOAP client hitting a fairly simple web service that I could peek at?
Thanks in advance.