Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

maven plugin to call or invoke a rest web service

Tags:

plugins

maven

Is there any maven plugin which will be invoking already existing rest web service? or else is there any way to invoke a web service in pom.xml. like we have for invoking a external command org.codehaus.mojo exec-maven-plugin 1.2 please help me

like image 685
pavan Avatar asked Sep 26 '11 07:09

pavan


1 Answers

If you need invoke a REST service using a POST method, you can use a groovy script

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.myspotontheweb.demo</groupId>
    <artifactId>maven-rest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <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>
                            import groovyx.net.http.RESTClient
                            import groovy.util.slurpersupport.GPathResult
                            import static groovyx.net.http.ContentType.XML

                            solr = new RESTClient('http://localhost:8080/solr/update')

                            def response = solr.post(
                                contentType: XML,
                                requestContentType: XML,
                                body: {
                                    add {
                                        doc {
                                            field(name:"id", "SOLR1000")
                                            field(name:"name", "Solr, the Enterprise Search Server")
                                            field(name:"manu", "Apache Software Foundation")
                                            field(name:"cat", "software")
                                            field(name:"cat", "search")
                                            field(name:"features", "Advanced Full-Text Search Capabilities using Lucene")
                                            field(name:"features", "Optimized for High Volume Web Traffic")
                                            field(name:"features", "Standards Based Open Interfaces - XML and HTTP")
                                            field(name:"features", "Comprehensive HTML Administration Interfaces")
                                            field(name:"features", "Scalability - Efficient Replication to other Solr Search Servers")
                                            field(name:"features", "Flexible and Adaptable with XML configuration and Schema")
                                            field(name:"features", "Good unicode support: héllo (hello with an accent over the e)")
                                            field(name:"price", "0")
                                            field(name:"popularity", "10")
                                            field(name:"inStock", "true")
                                            field(name:"incubationdate_dt", "2006-01-17T00:00:00.000Z")
                                        }
                                    }
                                }
                            )
                            log.info "Solr response status: ${response.status}"
                         </source>
                     </configuration>
                 </execution>
              </executions>
         </plugin>
    </plugins>
    </build>
</project>

The REST API example was taken from Hubert Klein Ikkink's blog:

http://mrhaki.blogspot.com/

like image 107
Mark O'Connor Avatar answered Oct 18 '22 15:10

Mark O'Connor