开发者

maven plugin to call or invoke a rest web service

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开发者_如何学C invoking a external command org.codehaus.mojo exec-maven-plugin 1.2 please help me


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>


To give an example of the Antrun Maven Plugin solution:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.8</version>
            <executions>
                <execution>
                    <id>notify</id>
                    <phase>compile</phase>
                    <goals><goal>run</goal></goals>
                    <configuration>
                        <target>
                            <get dest="target/counter" usetimestamp="false" 
                              src="https://c1.navrcholu.cz/hit?site=144925;t=lb14;ref=;jss=0"
                              verbose="off" quiet="true" ignoreerrors="true"/>
                        </target>
                        <failOnError>false</failOnError>
                    </configuration>
                </execution>
            </executions>
        </plugin>


I needed support for an untrusted HTTPs connection and none of the above approaches was supporting this easily. I found the Java curl library. Ant's Get task is nice but does not support this. The Java curl library is also offering a wider range of options and HTTP methods. For my purposes I have used this solution together with the Groovy Maven plugin:

                <plugin>
                    <groupId>org.codehaus.gmaven</groupId>
                    <artifactId>groovy-maven-plugin</artifactId>
                    <version>2.2-SNAPSHOT</version>
                    <executions>
                        <execution>
                            <phase>integration-test</phase>
                            <goals>
                                <goal>execute</goal>
                            </goals>
                            <configuration>
                                <source>
                                    import static org.toilelibre.libe.curl.Curl.$;

                                    try {
                                        $("curl -k " +
                                                "-o target/openapi.json https://localhost:1234/openapi");
                                    }
                                    catch (Exception e) {
                                        System.err.println(e)
                                    }
                                </source>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.toile-libre.libe</groupId>
                            <artifactId>curl</artifactId>
                            <version>0.0.29</version>
                        </dependency>
                    </dependencies>
                </plugin>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜