Utilizing a Spring Kafka listener to consume JSON data and automatically determine the corresponding domain object

I have a project where I need to process various types of JSON messages that will be published. Unfortunately, I cannot modify the publisher's code to include any headers. However, I am interested in utilizing @KafkaHandler to manage these different JSON messages and map them to domain objects. I came across some helpful resources on using https://github.com/spring-projects/spring-kafka/tree/master/samples/sample-02

Since I lack control over the publisher code, I am looking for guidance on creating a custom deserializer to handle multiple types of JSON data with @KafkaHandler. Are there any references or examples available for writing a custom deserializer in this scenario?

Answer №1

Using class level listeners without deserialization is like being stuck in a catch-22 situation.

In order to decide on the appropriate method to invoke, we must have the type information after deserialization; without deserialization, inferring the type becomes impossible.

One possible approach could be creating a wrapper for multiple JSON deserializers and either implementing a `try...until success` mechanism or analyzing the JSON content to determine heuristically which deserializer to utilize for a particular record.

if (json.contains("\\"foo\\":")) {
     return deserializeWithFooDeserializer(data, headers);
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I loop through JSON in AngularJS to populate fields without knowing the key value?

Here is the data structure that I'm working with: { "0": { "keyword": "flower", "short_desc": "Lorem ipsum dolor sit amet, consectetur adipiscing elit.", "pt_value": "5" }, "1": { "keyword": "tree", "short_desc": "Lorem ipsum dolor sit amet, consecte ...

Guide on how to generate a JSON array structure for a collection of Plain Old Java Objects (POJOs) utilizing the code

I've been searching for a solution to convert a list of POJOs to JSON. We've previously used Codehaus Jackson with Spring MVC. What I'm trying to achieve is not an AJAX call with the @ResponseBody action, but rather a utility method to conve ...

Is there a way to obtain the Vimeo Video Id in JSON format similar to the YouTube API?

Utilizing the YouTube API, I am able to retrieve channel data effortlessly. However, when it comes to Vimeo Video, are there any similar methods available? I require a solution resembling this format: https://www.example.com/vimeo-api/search?part=snippet ...

Is it possible to extract a specific value from JSON data by making an AJAX call or applying a filter to the fetched JSON data?

I have been utilizing a treeview template and successfully displaying all the data. However, I am facing an issue where I need to pass a value from index.php to getdata.php. I attempted using an AJAX filter on the index.php page and also tried sending a v ...

What is the alternative method of sending a POST request instead of using PUT or DELETE in Ember?

Is there a way to update or delete a record using the POST verb in Ember RESTAdapter? The default behavior is to send json using PUT or DELETE verbs, but those are blocked where I work. I was wondering if there's a way to mimic Rails behavior by send ...

There was an issue while trying to interpret the JSON

Working with Volley and org.json to retrieve a JSON from a user's timeline on Twitter. Received an "Error with Request" in onErrorResponse. The issue doesn't seem to be a URL problem as the desired JSON is visible in LogCat. Here is the error log ...

Using Python's json.dumps() to write JSON data to a CSV file

While working on writing to a CSV file, I encountered an issue with dealing with a large JSON string in one of the columns. I am looking for a way to prevent the commas within the JSON from being treated as separate values in the CSV file. I prefer not to ...

Creating JSON from variables in SQL SERVER using the FOR JSON AUTO command is a straightforward process that allows you

I'm currently working on a SQL Server 2016 query that involves: iterating through multiple rows extracting data into variables converting these variables into JSON objects for storage in the database Below is a simplified version of the code I have ...

I am currently attempting to extract data from a JSON file by using key names for reference, but I am running into issues when dealing with nested keys

Is there a better way to retrieve values from a JSON file by matching key names? The current method I am using does not seem to work with nested keys, so any suggestions on alternative approaches would be appreciated. // Sample .JSON file { "ro ...

Ways to retrieve specific data from a JSON object

With the help of NewtonSoft, I successfully convert JSON using the following code snippet: var jtoken = JObject.Parse(stringStuff); Console.WriteLine(jtoken.ToString()); The result is as follows: { "data": { "user": { " ...

What is the process of parsing JSON within a Postman workflow?

Here is the JSON data I am working with: { "id": "0001", "type": "models", "name": "model_auto", "paths": { "/api/v1/vehicleModels": { "get": { "summary": "Returns a list of car manufacturers", "description": "Returns a li ...

Utilizing jQuery DataTables for data fetched via Ajax calls

I am trying to showcase a specific collection of data from my MongoDb in a table using the jQuery DataTables plugin. However, I encountered an error message which says: Requested unknown parameter '0' for row0, column 0. For more information ...

Navigating JSON data to retrieve a specific property in AngularJS using a select form

Struggling with AngularJS and JSON. I have a form.html view where users can select their province. I have a Province JSON file for the select tag, but when storing in MySQL, I need the province Id. I tried using ng-value="province.id" in the option tag but ...

A guide on retrieving data from a remote database using PHP and integrating it into Android Studio

@Override public void onClick(View v) { Log.d("Onclick","Loaded"); name=txt1.getText().toString(); new task().execute(name); } class task extends AsyncTask<String,String,Void> { private ProgressDialog ...

Is there a way to confirm the presence of multiple attributes in a JSON format using JavaScript?

Currently, I am developing a module that processes multiple complex JSON files and requires a method to notify users if certain elements are missing. Although the current approach works, I can't shake the feeling that there must be a more efficient a ...

Transferring selected text to a Cordova application from a different application

Can I extract highlighted text from a different application, such as a browser, and send it to my Intel XDK Cordova app in JSON format for utilization? (Potentially utilizing a context menu) ...

Converting JSON to JavaScript Date using UTC time

I am struggling with formatting the dates in a JSON object to work with highcharts. The JSON looks like this: [ [ "26-Sep-14", 10 ], [ "29-Sep-14", 75 ] ] Highcharts requires dates to be in the format Date. ...

What is the best way to extract the country information from this JSON data?

Here is the data retrieved from an API using CURL: { "ip": "8.8.8.8", "hostname": "google-public-dns-a.google.com", "city": "Mountain View", "region": "California", "country": "US", "loc": "37.3860,-122.0838", "org": "AS15169 Google Inc.", ...

Unlocking the treasures of JSON data in JavaScriptDiscovering the secrets of extracting JSON

let info = { "@type": "Movie", "url": "/title/tt0443272/", "name": "Lincoln", "image": "https://m.media-amazon.com/images/M/MV5BMTQzNzczMDUyNV5BMl5BanBnXkFtZTcwNjM2ODEzOA ...

How can json attributes in java be validated against predefined values?

How can I enforce validation of JSON attributes against predefined values in Java? for example: { "operation": "ONE" } The only acceptable values for the operation attribute are ONE, TWO, THREE. Therefore, in the JSON layer, we need to validate thi ...