Spring Boot + Apache Kafka Tutorial - #12 - Create REST API to Send JSON Object

Welcome to Spring Boot + Apache Kafka Tutorial series. In this lecture, we will create a REST API to send a JSON message in a request. The REST API intern converts JSON into a User object and sends it to Kafka Producer. Kafka Producer intern sends that user object to the Kafka topic.

Lecture - #12 - Create REST API to Send JSON Object


Hi. Welcome back. In this lecture, we'll create a REST API to send a JSON object from the client. Well, if we can see this, you know, Kafka architecture here In previous lecture, we have created a KafkaProducer for JSON you know, serializer, right? In this lecture, we will create a simple REST API that will send a JSON object in a request, and then Kafka Producer will basically send that JSON object to the Kafka topic All right, great. Let's go head over to the IntelliJ idea. Well, head over to the controller package, right click on the controller package new and then choose a Java class and let's give class name as JsonMessageController hit enter and let's annotate this class with @RestControllerAnnotation and let's use one more annotation that is @RequestMapping annotation and then pass the base URL that is, /api/v1/kafka All right, and let's go, let's inject JsonKafkProducer so private and then JsonKafkProducer and this is kafkaProducer. And let's use constructor-based dependency injection, so let's generate the constructor. So again, we don't have to use @Autowired annotation here because this is spring bean and it has only one single parameter as constructor so spring will automatically take care of injecting this dependency. Now next, create a method over here public and then provide a return type as a ResponseEntity and then pass the string as a type. Okay. And then let's say the method name is something like publish and then pass user object as a parameter perfect. And let's annotate this method with @PostMapping annotation. So this will basically handle HTTP POSTrequest and then provide the URL that is /publish and let's use @RequestBody annotation to convert JSON into Java object automatically. And then let's call Kafka Producer and then it's method sendMessage and then pass user object it and then simply return a ResponseEntity.Ok(" Json message sent to kafka topic"); Okay perfect. Now we have created a simple Rest endpoint which will handle HTTP POST request and it will process the JSON object from the HTTP request Okay, perfect. Now what we will do, we will run the spring boot application and let's see how KafkaProducer will send JSON message to the Kafka topic. So let me stop the existing server and let me restart the spring boot application. And here you can see Spring Boot application is up and running in an embedded tomcat server on port 8080. Let's go ahead and let's use Postman Rest Client to call the REST API. So I'm going to open the postman client over here and use the HTTP Post method and then type http: localhost :8080/api/v1/kafka/publish Okay, so this is the URL. And then when you need pass JSON object in a HTTP request right. Go to the headers not headers go-to body and then raw and then choose content type as JSON and in a body, we need to pass the user JSON object Right So let's have ID so let's say "id":1, "firstName": let's say RAMESH and then user "lastName": let's say "fadatare" Okay, perfect. Let's go ahead and hit send button over here and there we go. We got a successful response that is 200 OK and the message is "Json message sent to the Kafka topic". Now, let's go ahead and let's see whether this JSON message, you know, has sent to the Kafka topic or not so we can verify it from the command line. So you can just type this command, okay and you can able to see this command will basically consume the message from the Kafka topic and here you can see this is the JSON object from the Kafka topic. It means that we have successfully written a Kafka Producer to produce the JSON message and send to that Kafka topic, isn't it? Now let's head over to the IntelliJ idea and if you go to console you can able to see there is an exception. So this Kafka Consumer we have created in one of the previous lectures right and this Consumer has subscribed to the topic javaguides and this javaguides topic has a, you know, the message of type string. but we have just sent to JSON right. and this consume method has a message of type string. This should be a user, right? but it has a string type. that way you can able to see the error in a console. So what we can do is we can basically create one more topic to send only the JSON messages. So go to KafkaTopicConfig and here let's create one more topic so let me simply copy this bean and paste it here and let's say javaguides_json and let's change the name as well that is javaguides_json topic. Now let's head over to the JSON Kafka Producer and let's change the topic name from javaguides to javaguides_json. Now this JSON Kafka Producer will send the message to this topic that is javaguides_json topic. Okay, now let's run the Spring Boot application, and let's see how this works. Now let me run the spring boot project and our spring boot application is up and running in an embedded on port 8080. Now, let's send the JSON I mean, let's call this REST API to JSON object in a request. So let me call the send button and there we go. Now, let's go to command line over here and here what we'll do we will simply pass the topic name that it is javaguides_json hit enter and there we go and here you can able to see topic name javaguides_json contains this JSON message. Okay. Now let's go to the IntelliJ idea and in a console you can never see, there are no exceptions. It means that we have created a separate Kafka topic to store the JSON messages. Okay. In the next lecture, we will create a Kafka Consumer to consume the JSON message and it will automatically convert JSON into Java object all right, great. I will see you in the next lecture.