简体   繁体   English

尝试从 json 响应中获取节点时出现 java 错误

[英]java error when trying to fetch node from json response

I am trying to retrieve details from a node within a json path using rest assured and java.我正在尝试使用放心和 java 从 json 路径中的节点检索详细信息。 However I keep getting the following error:但是我不断收到以下错误:

java.lang.NoClassDefFoundError: org/apache/groovy/io/StringBuilderWriter
    at io.restassured.internal.path.json.ConfigurableJsonSlurper.parseObject(ConfigurableJsonSlurper.groovy:202)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
    at io.restassured.internal.path.json.ConfigurableJsonSlurper.parse(ConfigurableJsonSlurper.groovy:105)
    at io.restassured.internal.path.json.ConfigurableJsonSlurper$parse.callCurrent(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:52)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:154)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
    at io.restassured.internal.path.json.ConfigurableJsonSlurper.parseText(ConfigurableJsonSlurper.groovy:83)
    at io.restassured.path.json.JsonPath$4$1.method(JsonPath.java:949)
    at io.restassured.path.json.JsonPath$ExceptionCatcher.invoke(JsonPath.java:984)
    at io.restassured.path.json.JsonPath$4.doParseWith(JsonPath.java:951)
    at io.restassured.path.json.JsonPath$JsonParser.parseWith(JsonPath.java:1031)
    at io.restassured.path.json.JsonPath.get(JsonPath.java:202)
    at uk.co.hermes.cucumber.utils.xxx.postAccessToken(xxx.java:86)
    at uk.co.hermes.cucumber.utils.xxx.unblockUser(xxx.java:27)

I know the issue is with this line in the code:我知道问题出在代码中的这一行:

String token = jsonPathEvaluator.get("token");

How can this be fixed?如何解决这个问题?

public void postAccessToken(){

    RestAssured.baseURI  = "https://xxx";

    JSONObject requestJsonBodyParams = new JSONObject();
    requestJsonBodyParams.put("test", "xxx");



    Response response = RestAssured.given()
            .contentType(jsonContentType).
                    body(requestJsonBodyParams.toString()).
                    when().
                    post("/");

    Assert.assertEquals(response.statusCode(), 200);

    JsonPath jsonPathEvaluator = response.jsonPath();
    String token = jsonPathEvaluator.get("token");
}

UPDATE:更新:

在此处输入图片说明

My answer assumes that you are using Maven/Gradle. 我的答案假设您正在使用Maven / Gradle。 Please let us know if this is not the case. 如果不是这种情况,请告诉我们。

This error is a sign of a missing dependency on the classpath, alternatively a version crash. 此错误表明缺少对类路径的依赖,或者版本崩溃。 Generally, this should not happen as dependencies you use should include transitive dependencies to its own dependencies. 通常,这不会发生,因为您使用的依赖项应包括传递性依赖项对其自身的依赖项。 As for restassured, it contains a dependency to groovy: 至于放心,它包含对groovy的依赖:

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy</artifactId>
</dependency>

Then, there might be several potential reasons for this happening: 然后,可能有几种可能的原因导致这种情况发生:

Either you have excluded the transitive dependency to groovy from your restassured dependency, or you have a dependency to groovy with a different version than what restassured expects (one where the class in question is not available). 您要么从可放心的依赖中排除了对groovy的可传递依赖,要么您对groovy的依赖与版本不同(而不是所保证的期望)(该类不可用)。 A third option is that you have multiple dependencies depending on groovy, with different versions. 第三种选择是您有多种依赖于Groovy的依赖关系,并且具有不同的版本。

If you are using maven, a quick way to check is through this command: 如果您使用的是maven,则快速检查方法是通过以下命令:

mvn dependency:tree -Dincludes=org.codehaus.groovy

If you can try this, please post the result. 如果可以尝试,请发布结果。

Also, let us know which version of RestAssured you are using. 另外,让我们知道您正在使用哪个版本的RestAssured。

Update: 更新:

From your dependency:tree output you can see that there are several groovy versions in play here. 从您的dependency:tree输出中,您可以看到这里有多个常规版本。 The 2.5.6 I guess is added as an attempt to fix this, you can remove that as it only gives you a version crash. 我想添加2.5.6是为了解决此问题, 您可以删除它 ,因为它只会给您版本崩溃。

But, the good parts: 但是,好的部分:

You have two dependencies: 您有两个依赖项:

net.serenity-bdd:serenity-core:1.5.2
net.serenity-bdd:serenity-rest-assured:2.0.48

These each have groovy dependencies, but for widely different versions, 2.4.11 and 2.5.5, respectively. 它们每个都具有常规的依赖关系,但是对于不同的版本,分别为2.4.11和2.5.5。 This leads to a version crash, as code that expects 2.5.5 suddenly has to work with 2.4.11. 这会导致版本崩溃,因为期望2.5.5的代码突然必须与2.4.11一起工作。

The class that is missing was added to the code base after version 2.4.11, so you have code that expects this class to be there, but it is not. 在版本2.4.11之后,缺少的类已添加到代码库中,因此您有希望该类存在的代码,但事实并非如此。

There are two ways of fixing this: 有两种解决方法:

Either add a explicit depenency to groovy, as this will override the version of any transitive dependencies: 可以为groovy添加显式依赖,因为这将覆盖任何可传递依赖项的版本:

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy</artifactId>
    <version>2.5.5</version>
</dependency>

This is NOT the best way to fix it though, since you will still have a dependency that is assuming that you are using version 2.4.11. 但是,这并不是修复它的最佳方法,因为您仍然会假设您正在使用2.4.11版本。

The proper way to fix this is to align the version of the two mentioned dependencies: 解决此问题的正确方法是对齐上述两个依赖项的版本:

net.serenity-bdd:serenity-core:1.5.2
net.serenity-bdd:serenity-rest-assured:2.0.48

Without knowing any details about this framework, I would assume that you should use the same version for these two dependencies. 在不了解有关此框架的任何细节的情况下,我假设您应该对这两个依赖项使用相同的版本。

Update #2: 更新#2:

A quick google search shows that these two dependencies seem to follow the same versioning scheme. 谷歌快速搜索显示这两个依赖项似乎遵循相同的版本控制方案。 To sum up, either upgrade serentiy-core to 2.0.48, or downgrade serenity-rest-assured to 1.5.2. 综上所述, 可以将Serentiy-Core升级到2.0.48,或者将Serenity-rest-assured降级到1.5.2。 Also, remove your dependency to groovy-all. 另外,删除对groovy-all的依赖。

Try adding groovy-all.jar to your classpath. 尝试将groovy-all.jar添加到您的类路径中。 If you are using maven add: 如果您使用的是maven,请添加:

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.5.6</version>
    <type>pom</type>
</dependency>

For gradle: 对于gradle:

compile group: 'org.codehaus.groovy', name: 'groovy-all', version: '2.5.6', ext: 'pom'

I was facing the same error and after deep diving in POM.xml i figured out there were multiple groovy dependency however removing this : 我遇到了同样的错误,在POM.xml中深入研究后,我发现存在多个常规依赖项,但是删除了这个:

Just worked like a charm. 就像魅力一样运作。 I am not sure if that would help but yes try removing multiples groovy dependency. 我不确定这是否有帮助,但是可以尝试删除多个常规依赖项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM