是否有任何maven插件将调用已存在的其他Web服务?或者有没有办法在pom.xml中调用Web服务。 就像我们调用外部命令一样 org.codehaus.mojo EXEC - Maven的插件 1.2 请帮帮我
答案 0 :(得分:7)
如果需要使用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来自Hubert Klein Ikkink的博客:
答案 1 :(得分:3)
您可以使用Ant's Get task调用REST Web服务(尽管它仅限于GET方法)。并使用Maven's Antrun plugin来调用您的Ant脚本。
答案 2 :(得分:2)
您可以使用rest-maven-plugin执行POST或GET(其他方法,如PATCH或PUT也可能会起作用)。
该插件可以POST一个文件,并将REST请求返回的结果保存到文件中,对文件集提供正常的maven支持,并重新生成相对于POSTed文件的结果文件名。
它还支持纯GET请求,并将结果存储到特定文件中。
支持标准REST查询属性,例如设置查询参数,标头参数和请求/响应媒体类型。
查看代码。最新发布的maven插件版本也已发布,可通过普通的Sonatype Nexus存储库获取。
这是一个将JSON Schema文档提交给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>
答案 3 :(得分:0)
给出一个Antrun Maven插件解决方案的示例:
<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>
答案 4 :(得分:0)
我需要对不受信任的HTTPs连接的支持,并且上述方法都无法轻松地支持此连接。我找到了Java curl库。 Ant的Get任务很好,但不支持此任务。 Java curl库还提供了更多的选项和HTTP方法。出于我的目的,我已将此解决方案与Groovy Maven插件一起使用:
<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>