如何使用放心调用web服务
How to invoke a webservice using rest assured
我是放心的新手,我在使用它来运行一个基本程序时遇到了麻烦。我刚刚创建了一个 Java 项目并添加了所需的放心罐子。
下面是我的简单代码:
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.RestAssured.*;
import com.jayway.restassured.matcher.RestAssuredMatchers.*;
import org.hamcrest.Matchers.*;
public class RestServiceTest
{
public static void main(String args[]) throws Exception
{
// RestAssured.baseURI="http://restcountries.eu/rest/v1/";
String str=RestAssured.get("http://restcountries.eu/rest/v1/name/Norway").asString();
System.out.println(str);
}
}
我还添加了一个 pom.xml 文件来添加以下依赖项。
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>1.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-external</artifactId>
<version>1.9</version>
</dependency>
当我 运行 上面的代码时,我遇到异常:
Exception in thread "main" java.lang.NoClassDefFoundError: groovyx/net/http/HTTPBuilder
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetPublicMethods(Unknown Source)
at java.lang.Class.getMethods(Unknown Source)
at org.codehaus.groovy.reflection.stdclasses.CachedSAMClass.getSAMMethod(CachedSAMClass.java:164)
at org.codehaus.groovy.reflection.ClassInfo.isSAM(ClassInfo.java:359)
at org.codehaus.groovy.reflection.ClassInfo.createCachedClass(ClassInfo.java:349)
at org.codehaus.groovy.reflection.ClassInfo.access0(ClassInfo.java:41)
at org.codehaus.groovy.reflection.ClassInfo$LazyCachedClassRef.initValue(ClassInfo.java:497)
at org.codehaus.groovy.reflection.ClassInfo$LazyCachedClassRef.initValue(ClassInfo.java:488)
at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:49)
at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:36)
at org.codehaus.groovy.reflection.ClassInfo.getCachedClass(ClassInfo.java:111)
at org.codehaus.groovy.reflection.ReflectionCache.getCachedClass(ReflectionCache.java:110)
at org.codehaus.groovy.reflection.CachedClass.initValue(CachedClass.java:209)
at org.codehaus.groovy.reflection.CachedClass.initValue(CachedClass.java:200)
at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:49)
at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:36)
at org.codehaus.groovy.reflection.CachedClass.getInterfaces(CachedClass.java:252)
at org.codehaus.groovy.reflection.CachedClass.<init>(CachedClass.java:238)
at org.codehaus.groovy.reflection.ClassInfo.createCachedClass(ClassInfo.java:352)
at org.codehaus.groovy.reflection.ClassInfo.access0(ClassInfo.java:41)
at org.codehaus.groovy.reflection.ClassInfo$LazyCachedClassRef.initValue(ClassInfo.java:497)
at org.codehaus.groovy.reflection.ClassInfo$LazyCachedClassRef.initValue(ClassInfo.java:488)
at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:49)
at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:36)
at org.codehaus.groovy.reflection.ClassInfo.getCachedClass(ClassInfo.java:111)
at org.codehaus.groovy.reflection.ReflectionCache.getCachedClass(ReflectionCache.java:110)
at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:180)
at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:216)
at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:228)
at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createNormalMetaClass(MetaClassRegistry.java:171)
at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createWithCustomLookup(MetaClassRegistry.java:161)
at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.create(MetaClassRegistry.java:144)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClassUnderLock(ClassInfo.java:253)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:285)
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.getMetaClass(MetaClassRegistryImpl.java:257)
at org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(InvokerHelper.java:875)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.initMetaClass(ScriptBytecodeAdapter.java:825)
at com.jayway.restassured.authentication.NoAuthScheme.$getStaticMetaClass(NoAuthScheme.groovy)
at com.jayway.restassured.authentication.NoAuthScheme.<init>(NoAuthScheme.groovy)
at com.jayway.restassured.RestAssured.<clinit>(RestAssured.java:317)
at RestServiceTest.main(RestServiceTest.java:16)
Caused by: java.lang.ClassNotFoundException: groovyx.net.http.HTTPBuilder
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 43 more
有人可以帮助我创建一个 Maven 项目还是简单地 java 项目并添加 pom.xml 文件?
我已经把maven加入环境变量了
您需要创建一个 maven 项目并将依赖项添加到 pom.xml 文件并构建 maven 项目。
您需要添加的其他依赖项:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
问题似乎是您缺少 Groovy 的 HTTP-Builder。
如果您已经在使用 Maven,请尝试将这些行添加到 Maven 的 "dependencies" 部分 pom.xml:
http://mvnrepository.com/artifact/org.codehaus.groovy.modules.http-builder/http-builder/0.6
<dependency>
<groupId>org.codehaus.groovy.modules.http-builder</groupId>
<artifactId>http-builder</artifactId>
<version>0.6</version>
</dependency>
否则,下载 .jar(相同 link)并将其添加到项目的 class 路径。
另请参阅 HTTP-Builder Wiki:
我是放心的新手,我在使用它来运行一个基本程序时遇到了麻烦。我刚刚创建了一个 Java 项目并添加了所需的放心罐子。 下面是我的简单代码:
import com.jayway.restassured.RestAssured;
import com.jayway.restassured.RestAssured.*;
import com.jayway.restassured.matcher.RestAssuredMatchers.*;
import org.hamcrest.Matchers.*;
public class RestServiceTest
{
public static void main(String args[]) throws Exception
{
// RestAssured.baseURI="http://restcountries.eu/rest/v1/";
String str=RestAssured.get("http://restcountries.eu/rest/v1/name/Norway").asString();
System.out.println(str);
}
}
我还添加了一个 pom.xml 文件来添加以下依赖项。
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<version>1.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.jersey.jersey-test-framework</groupId>
<artifactId>jersey-test-framework-external</artifactId>
<version>1.9</version>
</dependency>
当我 运行 上面的代码时,我遇到异常:
Exception in thread "main" java.lang.NoClassDefFoundError: groovyx/net/http/HTTPBuilder
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetPublicMethods(Unknown Source)
at java.lang.Class.getMethods(Unknown Source)
at org.codehaus.groovy.reflection.stdclasses.CachedSAMClass.getSAMMethod(CachedSAMClass.java:164)
at org.codehaus.groovy.reflection.ClassInfo.isSAM(ClassInfo.java:359)
at org.codehaus.groovy.reflection.ClassInfo.createCachedClass(ClassInfo.java:349)
at org.codehaus.groovy.reflection.ClassInfo.access0(ClassInfo.java:41)
at org.codehaus.groovy.reflection.ClassInfo$LazyCachedClassRef.initValue(ClassInfo.java:497)
at org.codehaus.groovy.reflection.ClassInfo$LazyCachedClassRef.initValue(ClassInfo.java:488)
at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:49)
at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:36)
at org.codehaus.groovy.reflection.ClassInfo.getCachedClass(ClassInfo.java:111)
at org.codehaus.groovy.reflection.ReflectionCache.getCachedClass(ReflectionCache.java:110)
at org.codehaus.groovy.reflection.CachedClass.initValue(CachedClass.java:209)
at org.codehaus.groovy.reflection.CachedClass.initValue(CachedClass.java:200)
at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:49)
at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:36)
at org.codehaus.groovy.reflection.CachedClass.getInterfaces(CachedClass.java:252)
at org.codehaus.groovy.reflection.CachedClass.<init>(CachedClass.java:238)
at org.codehaus.groovy.reflection.ClassInfo.createCachedClass(ClassInfo.java:352)
at org.codehaus.groovy.reflection.ClassInfo.access0(ClassInfo.java:41)
at org.codehaus.groovy.reflection.ClassInfo$LazyCachedClassRef.initValue(ClassInfo.java:497)
at org.codehaus.groovy.reflection.ClassInfo$LazyCachedClassRef.initValue(ClassInfo.java:488)
at org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:49)
at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:36)
at org.codehaus.groovy.reflection.ClassInfo.getCachedClass(ClassInfo.java:111)
at org.codehaus.groovy.reflection.ReflectionCache.getCachedClass(ReflectionCache.java:110)
at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:180)
at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:216)
at groovy.lang.MetaClassImpl.<init>(MetaClassImpl.java:228)
at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createNormalMetaClass(MetaClassRegistry.java:171)
at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.createWithCustomLookup(MetaClassRegistry.java:161)
at groovy.lang.MetaClassRegistry$MetaClassCreationHandle.create(MetaClassRegistry.java:144)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClassUnderLock(ClassInfo.java:253)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:285)
at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.getMetaClass(MetaClassRegistryImpl.java:257)
at org.codehaus.groovy.runtime.InvokerHelper.getMetaClass(InvokerHelper.java:875)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.initMetaClass(ScriptBytecodeAdapter.java:825)
at com.jayway.restassured.authentication.NoAuthScheme.$getStaticMetaClass(NoAuthScheme.groovy)
at com.jayway.restassured.authentication.NoAuthScheme.<init>(NoAuthScheme.groovy)
at com.jayway.restassured.RestAssured.<clinit>(RestAssured.java:317)
at RestServiceTest.main(RestServiceTest.java:16)
Caused by: java.lang.ClassNotFoundException: groovyx.net.http.HTTPBuilder
at java.net.URLClassLoader.run(Unknown Source)
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 43 more
有人可以帮助我创建一个 Maven 项目还是简单地 java 项目并添加 pom.xml 文件?
我已经把maven加入环境变量了
您需要创建一个 maven 项目并将依赖项添加到 pom.xml 文件并构建 maven 项目。
您需要添加的其他依赖项:
<dependency>
<groupId>com.jayway.restassured</groupId>
<artifactId>rest-assured</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>
问题似乎是您缺少 Groovy 的 HTTP-Builder。
如果您已经在使用 Maven,请尝试将这些行添加到 Maven 的 "dependencies" 部分 pom.xml:
http://mvnrepository.com/artifact/org.codehaus.groovy.modules.http-builder/http-builder/0.6
<dependency>
<groupId>org.codehaus.groovy.modules.http-builder</groupId>
<artifactId>http-builder</artifactId>
<version>0.6</version>
</dependency>
否则,下载 .jar(相同 link)并将其添加到项目的 class 路径。
另请参阅 HTTP-Builder Wiki: