Solved

Spring Controller - inheritance in request parameter

Posted on 2016-09-21
3
59 Views
Last Modified: 2016-10-01
I have the following two classes:

public class Admin {
    private String firstName;
    private String lastName;
    private int id;
    private int type;  // type 1 = admin, type 2 = local admin

    // getters and setters
}

public class Location_Admin extends Admin {
    private int locationId;  
    private String locationName;

    // getters and setters
}

I would like to pass the above class into a single controller method like this:
@RequestMapping(url="/user", method = POST, consumes = APPLICATION_JSON_VALUE)
public void createUser(@RequestBody Admin admin) {
     if (admin instanceof Admin) {
            createAdmin(admin);
     } else if (admin instanceof Location_Admin) {
            createLocationAdmin((Location_Admin)admin);
     }
}

However, when I tested, it always calls the createAdmin method even I provided the locationId and locationName in my JSON request.  May I know how can I call createLocalAdmin if we found locationName (which seems to be Location_Admin instead of Admin) in my method?  I don't want to separate into two request mapping urls because we want to have only single point for our front-end to call.

Thank you.
0
Comment
Question by:Peter Kwan
  • 2
3 Comments
 
LVL 15

Expert Comment

by:gurpsbassi
ID: 41808490
try this:


public void createUser(@RequestBody Admin admin) {
      if (admin instanceof Location_Admin) {
            createLocationAdmin((Location_Admin)admin);
     }
   else {
            createAdmin(admin);
     }
}
0
 
LVL 16

Author Comment

by:Peter Kwan
ID: 41811878
sorry. it does not work. my json is low the following:
{"firstName":"Declan","lastName":"Assyrian","type":2,"locationName":"London"}

the json is interpreted as admin rather than location_admin, so the location name is ignored.
0
 
LVL 15

Accepted Solution

by:
gurpsbassi earned 500 total points
ID: 41821564
I think this will require some work.

In the JSON I think you will need to pass in another attribute indicating where it is a admin or location_admin.
And then have a look at @JsonSubTypes for jackson (assuming you are using jackson).
I've not used the @JsonSubTypes before but I believe it is what you are looking for.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question