Search code examples
java-8rest-assuredjackson-databindrest-assured-jsonpath

Deserialization for list of objects is not working with Rest Assured Jsonpath and jdk8


I have a Rest Endpoint that returns the below response

{
  "incentives": [
    {
      "incentiveId": "271230",
      "effectiveDate": "2022-06-01T07:00:00.000+0000",
      "expiryDate": "2022-10-01T03:00:00.000+0000",
      "incentiveName": "$500 MAZDA LOYALTY REWARD PROGRAM",
      "incentiveAmount": 500,
      "incentiveType": "discount",
      "disclaimer": ""
    },
    {
      "incentiveId": "271231",
      "effectiveDate": "2022-06-01T07:00:00.000+0000",
      "expiryDate": "2022-10-01T03:00:00.000+0000",
      "incentiveName": "$500 MAZDA MILITARY APPRECIATION BONUS CASH",
      "incentiveAmount": 500,
      "incentiveType": "discount",
      "disclaimer": ""
    },
    {
      "incentiveId": "271229",
      "effectiveDate": "2022-06-01T07:00:00.000+0000",
      "expiryDate": "2022-07-01T03:00:00.000+0000",
      "incentiveName": "MAZDA MOBILITY PROGRAM CASH BONUS",
      "incentiveAmount": 1000,
      "incentiveType": "discount",
      "disclaimer": "Program Period is 6/1/2022 - 6/30/2022."
    },
    {
      "incentiveId": "271242",
      "effectiveDate": "2022-06-01T07:00:00.000+0000",
      "expiryDate": "2022-07-06T06:59:00.000+0000",
      "incentiveName": "$500 MAZDA LEASE TO LEASE LOYALTY REWARD PROGRAM",
      "incentiveAmount": 500,
      "incentiveType": "discount",
      "disclaimer": ""
    }
  ]
}

I am using Rest Assured Jsonpath to deserialize the list of incentives using the below lines of code

JsonPath jsonPathEvaluator = response.jsonPath();
List<Incentive> incentivesList = jsonPathEvaluator.getList("incentives", Incentive.class);

But the above lines of code is throwing the following exception

java.util.ServiceConfigurationError: com.fasterxml.jackson.databind.Module: Provider com.fasterxml.jackson.datatype.jdk8.Jdk8Module not found
    at java.base/java.util.ServiceLoader.fail(ServiceLoader.java:593)
    at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.nextProviderClass(ServiceLoader.java:1219)
    at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNextService(ServiceLoader.java:1228)
    at java.base/java.util.ServiceLoader$LazyClassPathLookupIterator.hasNext(ServiceLoader.java:1273)
    at java.base/java.util.ServiceLoader$2.hasNext(ServiceLoader.java:1309)
    at java.base/java.util.ServiceLoader$3.hasNext(ServiceLoader.java:1393)
    at com.fasterxml.jackson.databind.ObjectMapper.findModules(ObjectMapper.java:929)
    at com.fasterxml.jackson.databind.ObjectMapper.findModules(ObjectMapper.java:912)
    at com.fasterxml.jackson.databind.ObjectMapper.findAndRegisterModules(ObjectMapper.java:948)
    at io.restassured.path.json.mapper.factory.DefaultJackson2ObjectMapperFactory.create(DefaultJackson2ObjectMapperFactory.java:29)
    at io.restassured.path.json.mapper.factory.DefaultJackson2ObjectMapperFactory.create(DefaultJackson2ObjectMapperFactory.java:27)
    at io.restassured.common.mapper.factory.ObjectMapperFactory$create.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:148)
    at io.restassured.internal.path.json.mapping.JsonPathJackson2ObjectDeserializer.createJackson2ObjectMapper(JsonPathJackson2ObjectDeserializer.groovy:37)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.codehaus.groovy.runtime.callsite.PlainObjectMetaMethodSite.doInvoke(PlainObjectMetaMethodSite.java:43)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:193)
    at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:61)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:51)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:171)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:194)
    at io.restassured.internal.path.json.mapping.JsonPathJackson2ObjectDeserializer.deserialize(JsonPathJackson2ObjectDeserializer.groovy:44)
    at io.restassured.path.json.mapping.JsonPathObjectDeserializer$deserialize.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139)
    at io.restassured.internal.path.json.mapping.JsonObjectDeserializer.deserializeWithJackson2(JsonObjectDeserializer.groovy:109)
    at io.restassured.internal.path.json.mapping.JsonObjectDeserializer$deserializeWithJackson2.callStatic(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:55)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:217)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:240)
    at io.restassured.internal.path.json.mapping.JsonObjectDeserializer.deserialize(JsonObjectDeserializer.groovy:70)
    at io.restassured.path.json.JsonPath.jsonStringToObject(JsonPath.java:1093)
    at io.restassured.path.json.JsonPath.getList(JsonPath.java:400)

Below are the dependencies in my pom.xml file

<!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.1.0</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jdk8</artifactId>
    <version>2.6.3</version>
</dependency>

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
    <version>2.6.0</version>
</dependency>

Below is the java version on my machine

java version "1.8.0_333"
Java(TM) SE Runtime Environment (build 1.8.0_333-b02)
Java HotSpot(TM) Client VM (build 25.333-b02, mixed mode, sharing)

Could you please help me understand on what is wrong and on how to resolve this issue?


Solution

  • My issue got solved by adding the below dependency in the pom.xml file, I'm using Java 11 version. The below dependency is used to support JDK8 data types.

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.datatype/jackson-datatype-jdk8 -->
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-jdk8</artifactId>
        <version>2.13.3</version>
    </dependency>