0

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 ?

2
  • Which approaches did you try so far? Where did you fail? I am not sure, but mapping JSON of any structure to something in Java should be rather difficult. You only could map the JSON as raw string. Commented Jun 9, 2017 at 8:46
  • have tried mapping it to string, which didnt work Commented Jun 9, 2017 at 9:44

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

What implementation are you using - JERSEY ?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.