I am trying to call a POST endpoint and pass an abstract class as a parameter in the body of the request through Postman. Depending on the "type" the controller should know which class to use and deserialise, but unfortunately it doesn't.
Here is my Controller:
@RestController
@RequestMapping("/api/v1/auth")
public class UserController {
@PostMapping("/authenticate")
public String authenticate(@RequestBody ProviderCredentials providerCredentials) {
return "hello";
}
}
Here is my abstract class:
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.gson.JsonObject;
import com.test.utils.enums.LoginProvider;
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = EmailProviderCredentials.class, name = "email"),
@JsonSubTypes.Type(value = FacebookProviderCredentials.class, name = "facebook")
})
public abstract class ProviderCredentials {
public String testString;
public ProviderCredentials() {}
}
Here are my two classes that extend the abstract class :
public class EmailProviderCredentials extends ProviderCredentials {
public EmailProviderCredentials(){
}
}
public class FacebookProviderCredentials extends ProviderCredentials {
public FacebookProviderCredentials(){
}
}
I am using Postman to create the post request as shown below
and I get this error:
{
"timestamp": "2020-12-27T13:35:46.755+0000",
"status": 500,
"error": "Internal Server Error",
"message": "Type definition error: [simple type, class com.test.utils.objects.ProviderCredentials]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.test.utils.objects.ProviderCredentials` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information\n at [Source: (PushbackInputStream); line: 1, column: 1]",
"path": "/api/v1/auth/authenticate"
}