gpt4 book ai didi

java - 当 JSON 键以数字开头时,Rest-Assured IllegalArgumentException

转载 作者:行者123 更新时间:2023-12-01 13:18:29 25 4
gpt4 key购买 nike

我正在向我的服务器发出请求,JSON 响应如下所示:

{
"6269f15a0bb9b1b7d86ae718e84cddcd" : {
"attr1":"val1",
"attr2":"val2",
.
.
"attrx":"valx",
}
}

key 是“用户”的 MD5 哈希值(与本讨论无关)。我目前无法更改此 key 。

当我运行放心测试时,我的请求对象看起来像

{ 
"fields": [ "attr1","attr2",...,"attr3" ]
"users": [
{
"userId": "2f605845757870234d94ae14ca83c660"
}
]
}

当我运行以下命令时,出现运行时错误

Response response = 
given().
contentType("application/json").
request().body(requestObj).
when().
post(ipAddress).
then().
extract().response();


response.
then().
body("6269f15a0bb9b1b7d86ae718e84cddcd.attr1",equalTo("blah"));


java.lang.IllegalArgumentException: Invalid JSON expression:
Script1.groovy: 1: unexpected token: 6269f @ line 1, column 27.
6269f15a0bb9b1b7d86ae718e84cddcd
^

1 error

at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
at com.jayway.restassured.internal.path.json.JSONAssertion.getAsJsonObject(JSONAssertion.groovy:51)
at com.jayway.restassured.internal.path.json.JSONAssertion$getAsJsonObject.callCurrent(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:141)
at com.jayway.restassured.internal.path.json.JSONAssertion.getResult(JSONAssertion.groovy:31)
at com.jayway.restassured.path.json.JsonPath.get(JsonPath.java:183)
at com.nxn.AutomationTests.MDXTest.testSample(MDXTest.java:88)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

现在,我假设这是因为 JSON 被反序列化为 Java 类,当我请求 attr1 的值时,它会进行类查找,但由于类名不能以数字开头,该类查找失败。有谁知道我如何解决这个问题?

最佳答案

这实际上是一个错误!我已将其添加到问题跟踪器 ( https://code.google.com/p/rest-assured/issues/detail?id=318 )。

解决方法是转义属性名称:

.. .then().body("\"6269f15a0bb9b1b7d86ae718e84cddcd\".attr1"), equalTo("val1")

关于java - 当 JSON 键以数字开头时,Rest-Assured IllegalArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22257397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com