Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · บทเรียน

การแปลงข้อมูลกลับและการแปลงข้อความ

กำหนดค่าตัวแปลงข้อมูลกลับสำหรับรูปแบบข้อความต่าง ๆ เช่น String, JSON และอ็อบเจ็กต์แบบกำหนดเอง พร้อมจัดการการแปลงข้อความภายในตัวรับฟัง

บทเรียน 3 จาก 411 ขั้นตอน

การแปลงข้อมูลกลับและการแปลงข้อความ เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Kafka's Raw Messages

Kafka doesn't care about the format of your data. It treats every message as a raw array of bytes. This is how messages are stored and transmitted.

When a producer sends a message, it's first converted into bytes. When a consumer receives that message, it's still just a bunch of bytes.

This flexibility allows Kafka to handle any data type, but it means consumers need a way to interpret those bytes back into a usable format.

From Bytes to Objects: Deserialization

To make sense of Kafka messages in your Java application, you need to convert these raw bytes back into meaningful Java objects. This crucial process is called deserialization.

A deserializer is a specific component that knows how to read a byte array and reconstruct a Java object from it. Think of it as the reverse of serialization.

Without proper deserialization, your consumer would only ever see byte[], which isn't very useful for application logic.

Default String Deserialization

Spring for Apache Kafka provides sensible defaults to get you started quickly. If you don't specify otherwise, it assumes your message keys and values are simple strings.

Behind the scenes, it uses org.apache.kafka.common.serialization.StringDeserializer to convert message bytes into Java String objects.

This works perfectly for plain text messages, but most real-world applications often need to handle more structured data.

Code: Simple String Consumer

Here's a basic Spring Boot application with a Kafka listener that consumes simple string messages. Notice the String type in the listener method signature.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

@SpringBootApplication
public class SimpleConsumerApp {
  public static void main(String[] args) {
    SpringApplication.run(SimpleConsumerApp.class, args);
    System.out.println("String consumer app started, listening on my-string-topic...");
  }

  @Component
  static class MyStringListener {
    @KafkaListener(topics = "my-string-topic", groupId = "string-group")
    public void listen(String message) {
      System.out.println("Received String: " + message);
    }
  }
}

Configuring Deserializers Explicitly

You can explicitly configure which deserializers to use in your application.properties or application.yml file. This tells Spring Kafka which classes to use for converting bytes for both the message key and value.

  • spring.kafka.consumer.key-deserializer
  • spring.kafka.consumer.value-deserializer

For our default string example, these would be set to org.apache.kafka.common.serialization.StringDeserializer.

Working with JSON Data

JSON (JavaScript Object Notation) is a widely used format for exchanging structured data. It's very common for Kafka messages to carry data in JSON format.

To utilize JSON messages effectively in your Java application, you need to convert the incoming JSON string (or bytes) into a Java object, typically a Plain Old Java Object (POJO).

This mapping allows you to easily access data fields directly, like myObject.getId() or myObject.getName(), instead of parsing JSON manually.

Spring's JsonDeserializer

Spring for Apache Kafka provides a powerful org.springframework.kafka.support.serializer.JsonDeserializer to simplify handling JSON messages.

This deserializer leverages the popular Jackson library to automatically map incoming JSON bytes to your specified Java POJO class.

You just need to configure it to know which POJO type to expect, and it handles the complex conversion for you.

Code: Defining an Event POJO

First, let's define a simple Java POJO that will represent our incoming JSON data. The properties of this POJO should match the fields in your JSON messages.

package com.coddykit;

// MyEvent.java
public class MyEvent {
    private String id;
    private String description;

    // Default constructor is crucial for deserialization
    public MyEvent() {}

    public MyEvent(String id, String description) {
        this.id = id;
        this.description = description;
    }

    // Getters and Setters (omitted for brevity, but needed)
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getDescription() { return description; }
    public void setDescription(String description) { this.description = description; }

    @Override
    public String toString() {
        return "MyEvent{id='" + id + "', description='" + description + "'}";
    }
}

Code: Consuming JSON Messages

Now, let's update our consumer to use the JsonDeserializer and listen for MyEvent objects. Remember, you'll also need to configure the deserializer in your application.properties.

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import com.coddykit.MyEvent; // Import your POJO

@SpringBootApplication
public class JsonConsumerApp {
  public static void main(String[] args) {
    SpringApplication.run(JsonConsumerApp.class, args);
    System.out.println("JSON consumer app started, listening on my-json-topic...");
  }

  @Component
  static class MyJsonListener {
    @KafkaListener(topics = "my-json-topic", groupId = "json-group")
    public void listen(MyEvent event) {
      System.out.println("Received JSON Event: " + event.toString());
    }
  }
}
// Required application.properties configuration:
// spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
// spring.kafka.consumer.value-deserializer=org.springframework.kafka.support.serializer.JsonDeserializer
// spring.kafka.properties.spring.json.value.default.type=com.coddykit.MyEvent

Deserializer Configuration Quiz

Consider a Spring Boot Kafka consumer application that needs to process messages where the value is a JSON representation of a Product object, defined in com.example.Product. Which configuration is essential in application.properties for this setup?

Deserialization Recap

Great job! In this lesson, you learned that Kafka messages are raw bytes and require deserialization to be used in Java applications.

  • Spring Kafka uses StringDeserializer by default for simple text messages.
  • You can configure specific deserializers for keys and values using application.properties.
  • The JsonDeserializer simplifies mapping JSON messages to Java POJOs automatically.

Understanding deserialization is fundamental to building robust Kafka consumers that can handle various data formats effectively.

เริ่มต้นได้ฟรี

เรียนรู้ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การแปลงข้อมูลกลับและการแปลงข้อความ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแปลงข้อมูลกลับและการแปลงข้อความ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแปลงข้อมูลกลับและการแปลงข้อความ”

กำหนดค่าตัวแปลงข้อมูลกลับสำหรับรูปแบบข้อความต่าง ๆ เช่น String, JSON และอ็อบเจ็กต์แบบกำหนดเอง พร้อมจัดการการแปลงข้อความภายในตัวรับฟัง คุณปฏิบัติ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การแปลงข้อมูลกลับและการแปลงข้อความ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม

ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การสร้างคอนเทนเนอร์ตัวรับฟัง Kafka
  2. การจัดการกลุ่มผู้บริโภค
  3. การแปลงข้อมูลกลับและการแปลงข้อความ
  4. การรับข้อมูลเป็นชุดและโหมดการยืนยัน
← กลับไปที่ Advanced Spring Boot 4: Event-Driven Architecture (Kafka)