0

I have the following setup: A Rest endpoint to accept JSON POST request: I am parsing the request and sending it as a String over Kafka. But getting parsing errors A Rest endpoint to accept JSON POST request:

[ { "Name": "Jack", "Id": "314", "Gender": "M" } , { "Name": "John", "Id": "451", "Gender": "M" }, { "Name": "Rita", "Id": "501", "Gender": "F" } ]

I am parsing the request as follows

@RequestMapping(method = RequestMethod.POST, value = "/record")
@ResponseBody
public String process(@RequestBody Map<String, Object>[] payload) throws 
Exception {
    String str = Arrays.toString(payload)
    KafkaProd.toTopic(str);
    System.out.println("Payload: " +str);
    return "Record Processed";
}


str = Arrays.toString(payload) is changing it into the following format
[ { Name = Jack , Id = 314, Gender = M } , { Name = John, Id = 451, 
Gender = M }, { Name = Rita, Id = 501, Gender = F } ]

When I'm trying to parse this string back into json array using json-s 
imple :

 JSONArray jsonArray = new JSONArray(record.value());
 for (int i = 0; i < jsonArray.length(); i++) {
 System.out.println("Json Objects : " 
 +jsonArray.getJSONObject(i).toString());
 }

I am getting JSON Parsing error, since the record.value() is not a valid json array

Option 1. How do I convert this to a valid json array? Option 2. How do I send the json array in a proper format through kafka?

Which of these options do I use?

1 Answer 1

1

Instead of String str = Arrays.toString(payload) use a Jackson ObjectMapper to convert the map to a String.

Sign up to request clarification or add additional context in comments.

3 Comments

I wonder what is the point to convert an incoming JSON into Map and then back to JSON for sending to Kafka... What is the point to convert request byte[] to JSON at all since we can simply send that byte[] to Kafka?!
I was going to say the same, but I assumed this was just an example and his controller needs to examine the map contents.
I'm using byte[] bytes = new ObjectMapper().writeValueAsString(payload).getBytes("utf-8") Thanks Gary for pointing me in the right direction, Artem for the byte[] suggestion.

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.