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:
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:
An example taken from http://developer.yahoo.com/search/content/V1/termExtraction.htmlvirgin mary renaissance inspiration
This is one way to do that in Maven
Add some useful dependency
Specify the Groovy pluginorg.codehaus.groovy.modules.http-builder http-builder 0.5.1
Add add the proper Groovy codeorg.codehaus.groovy.maven gmaven-plugin 1.0 generate-resources execute ...
import groovyx.net.http.RESTClient import groovy.util.slurpersupport.GPathResult import static groovyx.net.http.ContentType.URLENC import static groovyx.net.http.ContentType.XML yahoo = new RESTClient('http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction') 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
That will produce the following output:4.0.0 test maven-rest 1.0.0 Maven Rest Example org.codehaus.groovy.modules.http-builder http-builder 0.5.1 org.codehaus.groovy.maven gmaven-plugin 1.0 generate-resources execute <![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 yahoo = new RESTClient('http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction') 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}" ]]>
pantinor@pantinor maven_rest$ mvn install | grep -i "yahoo" [INFO] yahoo response data: virgin maryrenaissanceinspiration
Nice post.Keep updating Devops Online Course
ReplyDelete