Object থেকে JSON এবং JSON থেকে Object Mapping

Spring OXM এবং JSON Integration - স্প্রিং ওএক্সএম (Spring OXM) - Java Technologies

295

Spring OXM (Object XML Mapping) মূলত XML ডেটা প্রসেসিংয়ের জন্য ব্যবহৃত হয়। কিন্তু যদি JSON এবং Object-এর মধ্যে Mapping করতে চান, তবে Spring Framework-এ Jackson, Gson, বা অন্য JSON প্রসেসর ব্যবহার করতে হবে। Spring OXM এটি সরাসরি সমর্থন করে না।

তবে Spring Context এবং Jackson Library ব্যবহার করে Object থেকে JSON এবং JSON থেকে Object Mapping করা যায়। এখানে উদাহরণ সহ প্রক্রিয়াটি ব্যাখ্যা করা হলো:


Dependency যুক্ত করা (Maven বা Gradle)

Maven:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.0</version>
</dependency>

Gradle:

implementation 'com.fasterxml.jackson.core:jackson-databind:2.15.0'

Object থেকে JSON Mapping (Serialization)

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) {
        try {
            // Sample Object
            User user = new User(1, "John Doe", "john.doe@example.com");

            // ObjectMapper তৈরি
            ObjectMapper objectMapper = new ObjectMapper();

            // Object থেকে JSON-এ রূপান্তর
            String jsonString = objectMapper.writeValueAsString(user);

            System.out.println("JSON String: " + jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

// Sample POJO Class
class User {
    private int id;
    private String name;
    private String email;

    // Constructors, Getters, and Setters
    public User(int id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }

    public User() {}

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Output:

JSON String: {"id":1,"name":"John Doe","email":"john.doe@example.com"}

JSON থেকে Object Mapping (Deserialization)

import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
    public static void main(String[] args) {
        try {
            // JSON String
            String jsonString = "{\"id\":1,\"name\":\"John Doe\",\"email\":\"john.doe@example.com\"}";

            // ObjectMapper তৈরি
            ObjectMapper objectMapper = new ObjectMapper();

            // JSON থেকে Object-এ রূপান্তর
            User user = objectMapper.readValue(jsonString, User.class);

            System.out.println("User Object: " + user.getName() + ", " + user.getEmail());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Output:

User Object: John Doe, john.doe@example.com

Spring Boot ব্যবহার করলে

Spring Boot প্রজেক্টে এটি আরও সহজ। @RestController এবং @RequestBody ব্যবহার করে সহজেই Object এবং JSON-এর মধ্যে Mapping করা যায়।

Controller উদাহরণ:

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class UserController {

    @PostMapping("/user")
    public User createUser(@RequestBody User user) {
        System.out.println("Received User: " + user.getName());
        return user; // Object থেকে JSON-এ রূপান্তর করে Response দেয়
    }
}

Spring Boot JSON Configuration:

Spring Boot স্বয়ংক্রিয়ভাবে Jackson ব্যবহার করে। আলাদা কিছু করতে হয় না।


সংক্ষেপে

  1. Spring OXM XML প্রসেসিংয়ের জন্য ব্যবহৃত হয়।
  2. JSON প্রসেসিংয়ের জন্য Jackson বা Gson ব্যবহার করতে হবে।
  3. Spring Boot ব্যবহার করলে সহজেই Object-JSON Mapping করা যায়।
Content added By
Promotion

Are you sure to start over?

Loading...