Reputation: 497
I am currently trying to get an auth token given some body of either application/json
data or application/www-x-urlformencoded
. This means that i'd be using an map or a POJO class respectively. The issue is that i am going to be receiving any variation of parameters that need to go into a POJO class. For example the auth request could include:
clientId :1234
clientSecret : 1235
or it can include something like
clientId:1235
clientSecret:1234
appid:21345
etc: etc
I have no way of predicting what will be in the body so i cant just make every variation of json body as a class.
Is there a way to dynamically create a class, or more preferably, emulate the class in a way that allows me to specify the member variables at runtime?
I need a way to create an object that i can include in my RestTemplate
exchange
function that has dynamically named member variables
Upvotes: 1
Views: 892
Reputation: 45
Do you need all the potential fields in your pojo? Or can you progress with a subset of known fields? Then you can just define the subset. If you use jackson as a json mapper, it will just discard additional fields as far as I know, unless you tell it to throw exceptions when json contains more fields than the pojo.
Maybe this is of help: https://www.baeldung.com/jackson-mapping-dynamic-object
Upvotes: 1