1

I have a REST API uploadFeed that uploads the user feed based on feedType (string value taken input as part of the request body) . Different feedtype provides a different pojo model in the request body altogether.

For e.g If the feedType is lets say "TYPE1", then the API's request-body should look like the following:

{
 "feedType":"TYPE1",
 "inputModel": {
    "a": "somevalue"
    "b" : "somevalue",
    "c" : "somevalue",
  }
}

if the feedType is lets say "TYPE2", then the API's request-body should look like the following:

{
 "feedType":"TYPE2",
  "inputModel": {
    "x": "somevalue"
    "y" : "somevalue",
    "z" : "somevalue",
  }
}

Now I want to avoid providing different REST API endpoints for the two feedTypes. I am thinking of rather providing a serialised object of the inputModel as input and then deserialise at the backend based on the feedType. For .e.g

{
 "feedType":"TYPE2",
  "inputModel": "<<serialized object>>"
}

Is this a good REST API design or is there any better alternative.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

0

You can create a class to map feedback request with Map field for inputModel,

class FeedbackRequest{
   String feedType;
   Map<String, String> inputModel;
   //getters setters
}
Vikas
  • 6,868
  • 4
  • 27
  • 41
0

You can user JSONObject

accept json as string at api and parse it to JSONObject and user it as per your logic

@RequestBody String jsonString
JSONObject jsonObj = new JSONObject(jsonString);

More on JSONObject here and here.

Hemant
  • 1,403
  • 2
  • 11
  • 21