0

I'm building an Angular app that communicates with a restful API written with the Spring Framework.

My class hierarchy is something like this:

class Resource
class FileResource extends Resource
class DbResource extends Resource
class DbFileResource extends DbResource
class DbServerResource extends DbResource

and I'm sending such a json object in my post request for example, which suites the DbServerResource in my class hierarchy:

{
"resourceName": "Res2",
"resourceType": "DB_SERVER",
"dbms": "DB2",
"dbName": "SHOP",
"host": "<some ip>",
"port": 50000,
"userName": "user",
"password": "password"
}

In my controller I have a I have addResource method:

@RequestMapping (method = RequestMethod.POST, value="/resource")
private void addResource(@RequestBody Resource resource)

And I have a Factory class that I'm calling to retrieve the exact object to work with by using instanceof:

public ResourceId getResourceId(Resource res){

    if (res== null){
        return null;
    }....

But when sending my request I am getting a NullPointerException:

java.lang.NullPointerException com...ResourceManager.validateResourceId(ResourceManager.java:861) com...management.resource.manager.ResourceManager.addResource(ResourceManager.java:127)

Seems that the Factory is not managing to recognize the relevant object and returning null! How shall spring recognize to which object it should map the json?! Or am I doing something else wrong!

Floern
  • 33,559
  • 24
  • 104
  • 119
zbeedatm
  • 601
  • 1
  • 15
  • 39
  • found something interesting, will give it a try: https://stackoverflow.com/questions/27170298/spring-reponsebody-requestbody-with-abstract-class – zbeedatm Nov 12 '17 at 08:24

1 Answers1

0

I tried using these annotation: https://fasterxml.github.io/jackson-annotations/javadoc/2.4/com/fasterxml/jackson/annotation/JsonTypeInfo.html

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, 
            include = JsonTypeInfo.As.PROPERTY, 
            property = "resourcetype",
            visible=true)
@JsonSubTypes({
@JsonSubTypes.Type(value = DbServerResource.class, name = "dbServerResource"),
@JsonSubTypes.Type(value = DbFileResource.class, name = "dbFileResource"),
@JsonSubTypes.Type(value = FileResource.class, name = "fileResource")
})

public abstract class DisplayResource{
protected String resourceName;
@JsonProperty(value = "resourceType") protected Types resourceType;

public enum Types { DB_SERVER, DB_FILE, FILE }  

but now when I'm sending my POST request I'm getting status 400 bad request!!!

My controller side now seems like this:

@RequestMapping (method = RequestMethod.POST, value="/resource")
private @ResponseBody <T extends DisplayResource>T addResource(@RequestBody T resource) 
        throws ResourceManagementException, ConfigurationException, MalformedURLException{

    logger.info("Adding a new resource: " + resource.toString());

    ...     
    resourceManagerService.addResource("admin", resourceFactory.getResourceId(resource), credentials);

    return resource;
}

The GET method is still working fine.

zbeedatm
  • 601
  • 1
  • 15
  • 39