REST API এর মাধ্যমে XML/JSON ডেটা প্রসেস করা

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

245

স্প্রিং ওএক্সএম (Spring OXM) এর মাধ্যমে REST API এর জন্য XML/JSON ডেটা প্রসেস করা বেশ সহজ। Spring OXM মূলত XML ডেটা প্রসেস করার জন্য ব্যবহৃত হয়, তবে REST API এ JSON এবং XML উভয় ডেটা হ্যান্ডল করার জন্য এটি Spring MVC-এর সাথে ইন্টিগ্রেট করা যেতে পারে।

কীভাবে REST API-তে XML/JSON ডেটা প্রসেস করবেন


১. প্রয়োজনীয় নির্ভরতাগুলো যোগ করা (Maven):

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.3.30</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-oxm</artifactId>
    <version>5.3.30</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>
<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>

২. জাভা মডেল ক্লাস তৈরি (JAXB ব্যবহার করে):

import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "employee")
public class Employee {
    private String name;
    private int age;

    @XmlElement
    public String getName() {
        return name;
    }

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

    @XmlElement
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

৩. কনফিগারেশন (Spring Config):

XML এবং JSON মার্শালিং এবং আনমার্শালিং এর জন্য কনফিগারেশন:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.xml.MarshallingHttpMessageConverter;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

import java.util.List;

@Configuration
public class AppConfig {

    @Bean
    public Jaxb2Marshaller jaxb2Marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setClassesToBeBound(Employee.class);
        return marshaller;
    }

    @Bean
    public MarshallingHttpMessageConverter marshallingHttpMessageConverter() {
        MarshallingHttpMessageConverter converter = new MarshallingHttpMessageConverter();
        converter.setMarshaller(jaxb2Marshaller());
        converter.setUnmarshaller(jaxb2Marshaller());
        return converter;
    }

    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(marshallingHttpMessageConverter());
    }
}

৪. REST API কন্ট্রোলার তৈরি:

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

@RestController
@RequestMapping("/api/employees")
public class EmployeeController {

    @PostMapping(consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE},
                 produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
    public Employee createEmployee(@RequestBody Employee employee) {
        // ডেটা প্রসেসিং বা সেভ করা
        employee.setName(employee.getName().toUpperCase()); // শুধু ডেমো প্রসেসিং
        return employee;
    }

    @GetMapping(produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
    public Employee getEmployee() {
        // উদাহরণস্বরূপ একটি Employee রিটার্ন
        Employee employee = new Employee();
        employee.setName("John Doe");
        employee.setAge(30);
        return employee;
    }
}

৫. API পরীক্ষা (Postman বা curl ব্যবহার করে):

XML ডেটা পাঠানো:

POST /api/employees
Content-Type: application/xml
Accept: application/xml

<employee>
    <name>Jane Smith</name>
    <age>25</age>
</employee>

JSON ডেটা পাঠানো:

POST /api/employees
Content-Type: application/json
Accept: application/json

{
    "name": "Jane Smith",
    "age": 25
}

রেসপন্স (XML/JSON):

XML:

<employee>
    <name>JANE SMITH</name>
    <age>25</age>
</employee>

JSON:

{
    "name": "JANE SMITH",
    "age": 25
}

মূল সুবিধা:

  1. XML এবং JSON উভয় ফরম্যাটে সাপোর্ট: Spring OXM-এর সাহায্যে REST API-তে XML ডেটা প্রসেসিং সহজ হয়, এবং Spring MVC-এর সাহায্যে JSON ডেটাও স্বয়ংক্রিয়ভাবে হ্যান্ডেল করা যায়।
  2. স্বয়ংক্রিয় মার্শালিং/আনমার্শালিং: OXM এবং Jackson-এর ইন্টিগ্রেশনের মাধ্যমে ডেটা রূপান্তর স্বয়ংক্রিয় হয়।
  3. উচ্চ কার্যক্ষমতা: Spring-এর বিল্ট-ইন স্ট্রিম-ভিত্তিক প্রসেসিং এবং ক্যাশিং কার্যক্ষমতা বাড়ায়।

আপনি যদি আরও নির্দিষ্ট কিছু জানতে চান, যেমন কাস্টম ডেটা প্রসেসিং বা এডভান্সড ফিচার, তাহলে বলুন!

Content added By
Promotion

Are you sure to start over?

Loading...