0

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

Postman screenshot

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"
}
Andreas
  • 1
  • 2
  • Well, when I create a small example only with Jackson, then your classes and annotations work fine. Can you check what request body Spring actually receives? Also, what is `/api/v1/auth/authenticate2`? – Tom Dec 27 '20 at 14:13
  • How can I check? sorry it should be /authenticate (edited it) – Andreas Dec 27 '20 at 14:17
  • You can try to use a String parameter instead of `ProviderCredentials`, it should contain the raw body of the request. Then you can check what it contains. – Tom Dec 27 '20 at 14:24
  • This is what the body has: providerCredentials = {"type": "facebook"} – Andreas Dec 27 '20 at 14:58
  • Does `new ObjectMapper().readValue("{ \"type\": \"facebook\" }", ProviderCredentials.class)` work with your setup (run that in your `main` method)? If it does, then there is something wrong in your setup, but that is hard to find out via Stack Overflow. You then would need to debug your Spring Controller. – Tom Dec 27 '20 at 15:29
  • it doesnt work. Gives me the same error. – Andreas Dec 28 '20 at 14:54
  • One thing I missed in your code is your imports. You're mixing Jackson and Gson. Stick to either of them and remove the other one. – Tom Dec 28 '20 at 17:30
  • Yeah, same I wasnt using the import from Gson – Andreas Dec 28 '20 at 19:19
  • Well, at least you now know your issue isn't Spring. Also, like I said, when I use the exact code you've posted here, then `new ObjectMapper().readValue("{ \"type\": \"facebook\" }", ProviderCredentials.class)` works fine for me. So you can check if there's something different in your code and the one you've posted and you can reproduce the issue without needing to send a POST request every time. Good luck on your bug hunt, hope it will be successful. Also I would consider removing GSON completely from your project, so it can't interfere with anything include Spring. – Tom Dec 28 '20 at 19:42
  • Thanks a lot. I will update the post if I find anything – Andreas Dec 29 '20 at 07:15
  • any solution @Andreas? – MyProblems May 02 '22 at 17:13

0 Answers0