2011-09-26 62 views
4

是否有任何Maven插件將調用已有的其他Web服務?或者有什麼方法可以在pom.xml中調用Web服務。 像我們有一個調用外部命令 org.codehaus.mojo 的exec-Maven的插件 1.2 請幫我maven插件調用或調用休息Web服務

回答

6

如果您需要使用POST方法來調用REST服務,您可以使用Groovy腳本

<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> 

REST API example從休伯特·克萊恩Ikkink的博客採取:

http://mrhaki.blogspot.com/

+0

是否有可能將groovy代碼提取到文件中? – aurelius

+0

當然,當你在它的時候,只需從Groovy Script中加載文件並評估它。 :) –

+0

@aurelius是的。我的例子顯示了一個內聯腳本,也可以列出一個文件路徑或URL。請看這裏的插件文檔:https://groovy.github.io/gmaven/groovy-maven-plugin/execute-mojo.html –

2

你可以使用rest-maven-plugin來執行POST或GET(以及其他方法,如PATCH或PUT也可能會起作用)。

該插件可以POST文件並將從REST請求返回的結果保存到文件中,具有對文件集的正常maven支持並重新映射相對於POSTed文件的結果文件名。

它還將支持純GET請求並將結果存儲到特定文件。

支持標準REST查詢屬性,例如設置查詢參數,標題參數和請求/響應媒體類型。

請參閱參考代碼。 Maven插件的最新發布版本也通過普通的Sonatype Nexus存儲庫發佈和提供。

下面是將JSON模式文檔提交給NodeJS REST服務的示例,該服務將返回由Faker模塊生成的JSON樣本數據。它將上傳./target/classes/json/faker目錄中與'* .json'匹配的所有文件,並將結果存放到./target/classes/json/examples目錄中。

請看下面的例子。

<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> 
+1

你應該提到,你是這個插件的作者。 –

+0

這個插件運行良好,除了這是一個讓你的cookie之間保持2次通話的痛苦。我的服務需要我首先調用「登錄」操作,然後使用會話cookie調用資源上的另一種方法,並且我不知道如何使用插件執行此操作。 – spi