Solved

Spring Controller - inheritance in request parameter

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

623 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