如何使用放心来调用Web服务

时间:2015-08-31 06:18:39

标签: java web-services maven 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.access$700(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$8.initValue(CachedClass.java:209)
    at org.codehaus.groovy.reflection.CachedClass$8.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.access$700(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$1.run(Unknown Source)
    at java.net.URLClassLoader$1.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添加到环境变量中。

2 个答案:

答案 0 :(得分:2)

您需要创建一个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>

答案 1 :(得分:1)

问题似乎是你缺少Groovy的HTTP-Builder。

如果您已经在使用Maven,请尝试将这些行添加到Maven pom.xml中的“dependencies”部分:

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(相同链接)并将其添加到项目的类路径中。

另请参阅HTTP-Builder Wiki:

https://github.com/jgritman/httpbuilder/wiki