Grails + Metro - Contract First Web Service

We've been trying to get full fledged web services working in Grails built in contract frist approach, it felt like it should be easy, as all the bits were there, but all of the plugins to date were more proof of concept vs Enterprise ready (e.g. no control over namespaces). After looking at many technologies, decided to give Metro a try.

So, the target was to get Metro running with the following key requirements:

  • WSDL First
  • Minimal change required to configure the web service
  • Bound to Grails services so we can re-use business logic and interact with domain classes

Step 1:  Grails App + Metro

Create a simple Grails application (working from the ground up is the best way to explain it, rather than trying to explain our existing application).

Download Metro Grails plugin:  https://jax-ws-commons.dev.java.net/grails/ and install it in grails app.

I used grails-metro-1.0.zip. If you are building the web service bottom-up, you can follow the example on the page above and create web services without further steps. 

 Since the plugin doesn't have the utility to create java artifacts from the wsdl, download Metro [https://metro.dev.java.net/] and unzip it. Note that the bin folder has wsimport scripts which does the reverse engineering.

Step 2: Generate java source files

From the bin folder execute

wsimport -s <srcFolder> -d <outputClassesFolder> <WSDLFile> 

For Example: wsimport -s src -d classes HelloWorld.wsdl

This generates all the java classes with the annotations required for the contract first web service. Move the java classes from the src folder to grails app src/java folder.

Step 3: Generate groovy service

Generate the groovy service

grails create-service Test 

Step 4: Add annotations in service groovy file

import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.WebServiceContext;

@WebService(serviceName = "HelloWorld",
            portName="HelloWorldPort",
            endpointInterface = "sample.HelloWorld",
            targetNamespace = "http://mycompany.com/HelloWorld/1.0",
            wsdlLocation = "http://localhost:<port>/HelloWorld.wsdl")

class TestService implements sample.HellWorld {

  @Resource
  private WebServiceContext context;
 
   public sample.HelloWorldRs process(
        sample.HelloWorldRq payload){
          sample.HelloWorldRs res = new sample.HelloWorldRs();
	  //Web service code here
          return res;
    }
}

Step 5: Run Grails application

Run the grails application

grails run-app 

Labels

contract contract Delete
first first Delete
web web Delete
service service Delete
wsdl wsdl Delete
metro metro Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Sep 16, 2008

    Nicola says:

    Hi, I'm following this tutorial I used wsdl from metro example: https://metro....

    Hi,

    I'm following this tutorial I used wsdl from metro example:

    https://metro.dev.java.net/getting-started/wsit-enabled-fromwsdl/etc/AddNumbers.wsdl

    I generated classes with wsimport and moved them to src/java. I created a service with grails (grails create-service AddNumbers)

    then I modified AddNumbersService.groovy as follow:

    import javax.annotation.Resource;
    import javax.jws.WebService;
    import javax.xml.ws.WebServiceContext;
    
    @WebService(serviceName = "AddNumbers",
                portName="AddNumbersPort",
                endpointInterface = "org.example",
                targetNamespace = "http://example.org",
                wsdlLocation = "http://localhost:80/AddNumbers.wsdl")
    
    
    
    class AddNumbersService implements org.example.AddNumbers {
       
        boolean transactional = true
        @Resource
        private WebServiceContext context;
       
        public org.example.AddNumbersResponse process(
            org.example.AddNumbersService payload){
            org.example.AddNumbersResponse res = new org.example.AddNumbersResponse();
            //Web service code here
            return res;
        }  
    }
    

    I suspect there is a mistake with names (maybe with request definition), however I'm unable to find the problem, please be patient this is my start with java,grails and web services,

    I need to expose this service

    can you give me some hints?

    thanks

    Nicola