Friday, November 9, 2012

REST Invocation With Maven

Recently I had to invoke some REST service in one of my Maven builds.

I had never did it before but I was expecting to find some ready to use component that could allow me to do all the things that I needed directly from configuration but it wasn't the case.

I did the typical search to see what was available and I have found some good tip here and there.

But the conclusion is that there is not a ready "off the shelf" component.

The 2 most practical options seem to be:

- use an Ant GET Task
- embed Groovy code in you r script

Since the first option work only with the verb GET I decided to use the Groovy approach.

Let's say that we want to invoke a service via POST like this:

 
1
2
3
4
pantinor@pantinor maven_rest$
    curl -d appid=YahooDemo -d query=madonna -d \
    context="renaissance favored the Virgin Mary for inspiration" \
    http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction


Produces this output:
1
2
3
4
5
6
7
<resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:yahoo:cate" xsi:schemalocation="urn:yahoo:cate
<result>virgin mary</result>
<result>renaissance</result>
<result>inspiration</result>
</resultset>
<!-- ws03.ydn.ac4.yahoo.com uncompressed/chunked Thu Nov  8 23:04:36 PST 2012 -->
An example taken from http://developer.yahoo.com/search/content/V1/termExtraction.html


This is one way to do that in Maven

Add some useful dependency
1
2
3
4
5
6
7
<dependencies>
        <dependency>
            <groupid>org.codehaus.groovy.modules.http-builder</groupid>
            <artifactid>http-builder</artifactid>
            <version>0.5.1</version>
        </dependency>
</dependencies>
Specify the Groovy plugin
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<plugin>
 <groupid>org.codehaus.groovy.maven</groupid>
 <artifactid>gmaven-plugin</artifactid>
 <version>1.0</version>
 <executions>
  <execution>
   <phase>generate-resources</phase>
   <goals>
    <goal>execute</goal>
   </goals>
   <configuration>
    <source>
    ...
     
   </configuration>
  </execution>
 </executions>
</plugin>
Add add the proper Groovy code

1
2
3
4
5
6
7
8
9
10
11
12
13
import groovyx.net.http.RESTClient
import groovy.util.slurpersupport.GPathResult
import static groovyx.net.http.ContentType.URLENC
import static groovyx.net.http.ContentType.XML
 
 
def response = yahoo.post(
    contentType: XML,
    requestContentType: URLENC,
    body: [appid:"YahooDemo", query:"madonna", context:"renaissance favored the Virgin Mary for inspiration"]
)
log.info "yahoo response data: ${response.data}"

This is the full pom.xml file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 
 <modelversion>4.0.0</modelversion>
 <groupid>test</groupid>
 <artifactid>maven-rest</artifactid>
 <version>1.0.0</version>
 
 <name>Maven Rest Example</name>
 
 <dependencies>
        <dependency>
            <groupid>org.codehaus.groovy.modules.http-builder</groupid>
            <artifactid>http-builder</artifactid>
            <version>0.5.1</version>
        </dependency>
    </dependencies>
 <build>
  <plugins>  
 
   <plugin>
    <groupid>org.codehaus.groovy.maven</groupid>
    <artifactid>gmaven-plugin</artifactid>
    <version>1.0</version>
    <executions>
     <execution>
      <phase>generate-resources</phase>
      <goals>
       <goal>execute</goal>
      </goals>
      <configuration>
       <source>
        <![CDATA[
import groovyx.net.http.RESTClient
import groovy.util.slurpersupport.GPathResult
import static groovyx.net.http.ContentType.URLENC
import static groovyx.net.http.ContentType.XML
 
 
def response = yahoo.post(
    contentType: XML,
    requestContentType: URLENC,
    body: [appid:"YahooDemo", query:"madonna", context:"renaissance favored the Virgin Mary for inspiration"]
)
log.info "yahoo response data: ${response.data}"
        ]]>
        
      </configuration>
     </execution>
    </executions>
   </plugin>
  </plugins>
 </build>
 
 
</project>
That will produce the following output:

1
2
3
pantinor@pantinor maven_rest$
    mvn install | grep -i "yahoo"
    [INFO]  yahoo response data: virgin maryrenaissanceinspiration