Reputation: 959
I have 3 classes. A is abstract class, B and C extends A. I want to be able to get a JSON in a POST (A) and then to decide what type it is and to convert to the correct type. in other words to use only one end point for both B and C. The same I want to do in GET, I want to get a one list that contains B and C objects. Does it possible?
Upvotes: 1
Views: 1942
Reputation: 7166
A, B and C usually have names, like Animal, Dog and Cat.
Therefore those are different resources like /animal
, /animal/dog
and /animal/cat
. Following this rule, you can determine the type by the requestUrl. A http-post
for /animal/dog
should result in a dog
resource and a /animal/cat
in a cat
. Both http-post
could use the same JSON of course.
For http-get
, the requested resouce might be /animals
containing a combined list of the resources /animal/cats
(list of all /animal/cat
) and /animal/dogs
(a list of all /animal/dog
)
If you go down the path your are mentioning in your question, you can just dump everything to /stuff
like cats, dogs, tricycles, contracts, space ships ... Not very restful.
Upvotes: 0
Reputation: 163
Yes, it is possible, what you want is to be able to handle polymorphic types. There are many frameworks that could help you, I personally use jackson 2, you can check here for an example:
https://github.com/FasterXML/jackson-annotations#handling-polymorphic-types
Upvotes: 2