Deserializing a json to an abstract class in spring cloud functions

408 Views Asked by At

I have a java project written using spring cloud functions and deployed in aws lambda. I have a scenario wherein I have an abstract class extended by 3 other classes.

The function catering to the requests receives a request in a json format. The request has data which is appropriate for one of the implementations of the abstract class(might be appropriate for a different implemetation at different times). If the deserialization is successful, the data needs to be persisted in db using spring-jpa.

As the deserialization proceeds automatically, it fails since abstract classes can't be instantiated and deserialization itself will not be able to identify the correct implementation.

Here are my class details:

Class A{

    String x;
    String y;
    .
    .
    .
    B b
    .
    .
    
    //gettters and setters



}


public class B {

    String type;
    private C c

}

public abstract class C{

}

public class D extends C{

    string var1;
    string var2;
    
    //setters and getters
}

public class E extends C{

    String var3;
    String var4;
    String var5;
    
    //setters and getters
}

and the request json that the function gets

{
    "x": "some x value",
    "y": "some y value",
    "b": {
        "type" : "some type",
        "c": {
            "var1": "some var1 value", //these would be three in case of  E
            "var2": "some var2 value"
        }
    }

}

The function that receives the request

public Function<A, A> getResponse(){
//some code
}

Since class C is abstract, deserialization fails as it's not able to decide which of the subclasses should be instantiated. Can someone suggest how to deal with this?

0

There are 0 best solutions below