maven plugin to call or invoke a rest web service

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/


You can call REST web service using Ant's Get task (though it's limited to only GET method). And use Maven's Antrun plugin to call your Ant script.


You can use the rest-maven-plugin to perform either a POST or a GET (and other methods such as PATCH or PUT would probably work as well).

The plugin can POST a file and also save the results returned from the REST request to a file, with normal maven support for filesets and remapping the resulting filenames relative to the POSTed file.

It will also support pure GET request with the results stored to a specific file.

Standard REST query properties are supported, such as setting Query parameters, Header parameters, and Request/Response media types.

See for the code. The latest release version of the maven plugin is also published and available via normal Sonatype Nexus repository.

Here's an example where JSON Schema document is submitted to a NodeJS REST service which will return JSON sample data generated by the Faker module. It will upload all the files in the ./target/classes/json/faker directory which match '*.json' and deposit the results into the ./target/classes/json/examples directory.

Check out the example below.

<properties>
  <rest-maven-plugin.version>1.4</rest-maven-plugin.version>
</properties>

<plugins>
  <plugin>
     <groupId>com.github.cjnygard</groupId>
     <artifactId>rest-maven-plugin</artifactId>
     <version>${rest-maven-plugin.version}</version>
     <executions>
       <execution>
         <id>fake-json-data</id>
         <phase>process-classes</phase>
         <goals>
           <goal>rest-request</goal>
         </goals>
         <configuration>
           <endpoint>${json-data-server.url}</endpoint>
           <resource>schema/v1/api</resource>
           <queryParams>
             <addRequired>1</addRequired>
           </queryParams>
           <fileset>
             <directory>${project.build.resourcepath}/json/faker</directory>
             <includes>
              <include>*.json</include>
             </includes>
           </fileset>
           <requestType>
             <type>application</type>
             <subtype>json</subtype>
           </requestType>
           <responseType>
             <type>application</type>
             <subtype>json</subtype>
           </responseType>
           <outputDir>${project.build.resourcepath}/md/json/examples</outputDir>            
         </configuration>
       </execution>
     </executions>
   </plugin>
</plugins>

Tags:

Plugins

Maven