是否有任何Maven插件將調用已有的其他Web服務?或者有什麼方法可以在pom.xml中調用Web服務。 像我們有一個調用外部命令 org.codehaus.mojo 的exec-Maven的插件 1.2 請幫我maven插件調用或調用休息Web服務
回答
如果您需要使用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的博客採取:
可以調用使用Ant's Get task REST Web服務(儘管它僅限於GET方法)。並使用Maven's Antrun plugin來調用您的Ant腳本。
你可以使用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>
你應該提到,你是這個插件的作者。 –
這個插件運行良好,除了這是一個讓你的cookie之間保持2次通話的痛苦。我的服務需要我首先調用「登錄」操作,然後使用會話cookie調用資源上的另一種方法,並且我不知道如何使用插件執行此操作。 – spi
- 1. 如何從jaspersoft Studio Proffessional調用Web服務或休息api?
- 2. WCF,Web服務或休息
- 3. 從CRM插件調用Web服務
- 4. 如何使用海報或任何插件調用Web服務?
- 5. 調用使用JQuery Ajax調用休息Web服務,Web服務返回JSON字符串
- 6. 使用AngularJS或如何從CustomApp調用休息服務
- 7. jQuery插件在ajax調用後休息
- 8. 如何從swift調用休息Web服務
- 9. 休息web服務調用在AngularJS(許諾)
- 10. 調用Web服務
- 11. 調用Web服務
- 12. 調用Web服務
- 13. 調用Web服務
- 14. 其他Web服務方法調用和非休息Web服務方法調用或常規方法調用之間的區別
- 15. Web API調用Web服務
- 16. 用於序列化Web服務調用的Weblogic插件
- 17. 休息服務調用的顯式事務處理
- 18. Web服務調用返回
- 19. 從httpmodule調用Web服務
- 20. 調用Web服務錯誤
- 21. AJAX調用Web服務
- 22. Outlook Web服務調用
- 23. 跨域Web服務調用
- 24. 從jquery調用web服務
- 25. 從PHP調用Web服務?
- 26. JQuery:調用Web服務
- 27. 無法調用Web服務
- 28. Aynchronous Web服務調用
- 29. 從Java調用Web服務
- 30. 多個web服務調用
是否有可能將groovy代碼提取到文件中? – aurelius
當然,當你在它的時候,只需從Groovy Script中加載文件並評估它。 :) –
@aurelius是的。我的例子顯示了一個內聯腳本,也可以列出一個文件路徑或URL。請看這裏的插件文檔:https://groovy.github.io/gmaven/groovy-maven-plugin/execute-mojo.html –