0
0
Spring Bootframework~5 mins

Message serialization in Spring Boot

Choose your learning style9 modes available
Introduction

Message serialization converts data into a format that can be easily sent over a network or saved. It helps different parts of a system understand each other by using a common data format.

When sending data between a client and server in a web application.
When storing data in a file or database in a structured way.
When communicating between microservices that use different programming languages.
When caching objects to quickly retrieve them later.
When logging complex data structures for debugging.
Syntax
Spring Boot
public class User implements Serializable {
    private String name;
    private int age;

    // getters and setters
}
Use the Serializable interface to mark a class as serializable.
Spring Boot often uses JSON format for serialization automatically in REST APIs.
Examples
This class can be serialized to send or save user data.
Spring Boot
public class User implements Serializable {
    private String name;
    private int age;

    // constructors, getters, setters
}
Spring Boot automatically serializes the User object to JSON when returning from a REST endpoint.
Spring Boot
@RestController
public class UserController {

    @GetMapping("/user")
    public User getUser() {
        return new User("Alice", 30);
    }
}
Manually converting a User object to JSON string using Jackson's ObjectMapper.
Spring Boot
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user);
Sample Program

This Spring Boot app has a REST endpoint /user that returns a User object. Spring Boot automatically converts the User object into JSON format when sending the response.

Spring Boot
package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

class User {
    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

@RestController
class UserController {
    @GetMapping("/user")
    public User getUser() {
        return new User("Alice", 30);
    }
}
OutputSuccess
Important Notes

Spring Boot uses Jackson library by default to convert Java objects to JSON and back.

Make sure your classes have getters for fields to be serialized properly.

For custom serialization, you can use annotations like @JsonProperty.

Summary

Message serialization turns objects into a format like JSON for easy sharing.

Spring Boot handles serialization automatically in REST APIs.

Ensure your data classes have proper getters and follow Java bean conventions.