Vineet Singla
Vineet Singla

Reputation: 1677

How to map JSON request to String using SPRING REST

I have a REST API with uri :

POST
/version/path/generic
Consumes : Application/JSON

The JSON can be of different format. Any valid JSON can be input of this API. So to support this in the webmethod, how can we map it in JAVA ?

String 
Object
MAP

Not getting proper json in any of the above formats ?

Upvotes: 0

Views: 1250

Answers (2)

aexellent
aexellent

Reputation: 144

You can map a method parameter like that:

@RequestBody String json

and use a JSON parser, for example Gson to map it to JAVA classes afteron like that:

Gson gson = new Gson();
SomeJavaClass jsonMapping = gson.fromJson(json, SomeJavaClass.class);

Upvotes: 1

Joachim Nielandt
Joachim Nielandt

Reputation: 155

Make an abstraction in Java, where you use a Map for the generic key-value mapping of a JSON object, a List for JSON arrays and regular String / Integer for JSON values.

Upvotes: 0

Related Questions