Solved

Spring Controller - inheritance in request parameter

Posted on 2016-09-21
3
91 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

More Than Just A Video Library

Train for your certification. Learn the latest DevOps tools. Grow your skillset to do better work.

At Linux Academy, we release new training modules every week so you'll always be up to date on the latest tech.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

729 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