com.fasterxml.jackson.databind.JsonMappingException due to new maven entries

时间:2017-06-15 10:31:40

标签: java json maven jackson rest-assured

Below is the test code which was working absolutely fine until now, but it is throwing below mentioned exception now.

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import static io.restassured.RestAssured.given;
import io.restassured.response.Response;

RequestSpecification spec = new RequestSpecBuilder().setBaseUri(someBaseUri).setBasePath(someEndpoint).build();
JsonParser parser = new JsonParser();
JsonObject jsonObject = parser.parse("{\r\n\"otp\":\""+sentOtp+"\",\r\n\"otpToken\":\""+otpToken+"\"\r\n}").getAsJsonObject();
response = given().spec(spec).headers("key","value").body(jsonObject).when().post(); //the exception is thrown at this line

Below is the exception trace:

com.fasterxml.jackson.databind.JsonMappingException: JsonObject (through reference chain: com.google.gson.JsonObject["asString"])
    at com.google.gson.JsonElement.getAsString(JsonElement.java:191)
    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 com.fasterxml.jackson.databind.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:679)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:534)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:597)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:142)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValue(ObjectMapper.java:1819)
    at com.fasterxml.jackson.databind.ObjectMapper$writeValue$0.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:133)
    at io.restassured.internal.mapping.Jackson2Mapper.serialize(Jackson2Mapper.groovy:53)
    at io.restassured.internal.mapping.Jackson2Mapper.serialize(Jackson2Mapper.groovy)
    at io.restassured.mapper.ObjectMapper$serialize.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at io.restassured.internal.mapping.ObjectMapping.serializeWithJackson2(ObjectMapping.groovy:181)
    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.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite$StaticMetaMethodSiteNoUnwrapNoCoerce.invoke(StaticMetaMethodSite.java:151)
    at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.callStatic(StaticMetaMethodSite.java:102)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:56)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:194)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:214)
    at io.restassured.internal.mapping.ObjectMapping.serialize(ObjectMapping.groovy:130)
    at io.restassured.internal.mapping.ObjectMapping$serialize.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at io.restassured.internal.RequestSpecificationImpl.body(RequestSpecificationImpl.groovy:829)
    ...

On some isolation and reviewing the commit history of my test project, I found that removing below chunk of code magically resolves the issue, while placing it back in pom.xml makes me run into the original issue again.

 <dependency>
     <groupId>io.rest-assured</groupId>
     <artifactId>json-schema-validator</artifactId>
     <version>3.0.3</version>
  </dependency>
  <dependency>
     <groupId>com.github.fge</groupId>
     <artifactId>json-schema-validator</artifactId>
     <version>2.2.6</version>
  </dependency>
  <dependency>
     <groupId>com.github.fge</groupId>
     <artifactId>json-schema-core</artifactId>
     <version>1.2.5</version>
  </dependency>

Please help.

1 个答案:

答案 0 :(得分:0)

如果你想传递Gson,你必须指定一个序列化器明确:

given().log().all().contentType(ContentType.JSON).headers("key", "value").body(jsonObject, ObjectMapperType.GSON).when().post();

实际上contentType(ContentType.JSON)会使你的输出变得美化