Solved

Spring Controller - inheritance in request parameter

Posted on 2016-09-21
3
37 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 …
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now