Solved

Spring Controller - inheritance in request parameter

Posted on 2016-09-21
3
86 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

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 16

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

732 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