Spring and Groovy, again…
Posted by ice09 on August 9, 2009
No real news here, just another example for Spring and Groovy in a simple web project.
What does it do?
We needed a simple Request/Response Mock for SOAP Web Services. Since the real sending component uses EJB and Weblogic t3-protocol specific stuff, I wanted to decorate this component.
The simplest way seemed to create an own endpoint, which accepts the SOAP web service requests, searches for templates based on the payload-root-element and evaluates the determined template (which itself is a Groovy template).
How does it work?
Quite straightforward. If a directory with the file template.groovy and the name of the root element of the payload exists, the template is loaded and evaluated with the input data and finally returned by the DispatcherServlet.
Spring MVC is used, however it was not necessary for this little sample.
The most interesting part is the injection (with @Autowired) of the Groovy script (which resides in src/main/resources/, which is not really correct).
<!-- this is really important to start the annotation processing in Spring --> <context:annotation-config/> <!-- the controller is instantiated, via @Autowired the Groovy Script is set --> <bean id="RequestMockController" class="com.mock.sample.RequestMockController"/> <!-- local repository (must be changed most likely) --> <bean id="localRepo" class="java.lang.String"> <constructor-arg value="c:/temp/repo/"/> </bean> <!-- instantiate Groovy as a Spring bean --> <lang:groovy id="xml" script-source="classpath:XmlToolImpl.groovy"/>
IMPORTANT: to be able to use Groovy-Spring beans, the Groovy object should implement a Java interface (which is then used in the Java code).
How do I test it?
Use the client code with the parameters supplied in the README.
The request could look like this:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wss="http://wsssample.com.mock.sample"> <soapenv:Header/> <soapenv:Body> <wss:getString> <wss:name>?</wss:name> </wss:gettring> </soapenv:Body> </soapenv:Envelope>
For this request to succeed, the file REPO/getString/template.groovy must exist (see resources).
In the sample, there is one special behaviour, which requires two passes – if the template returns something like “>redirect”, the request is redirected to the new file (this works one time only and is just included to easy the “dispatching or controller pattern”)