JSON/XML Response তৈরি করা

Request এবং Response Handling - স্প্রিং এমভিসি (Spring MVC) - Java Technologies

273

Spring MVC তে JSON বা XML রেসপন্স তৈরি করা খুবই সহজ, বিশেষ করে যদি আপনি Spring Boot ব্যবহার করেন, কারণ Spring Boot এটি স্বয়ংক্রিয়ভাবে সাপোর্ট করে। এখানে আমি আপনাকে JSON এবং XML রেসপন্স তৈরির প্রক্রিয়া ব্যাখ্যা করব।

Spring MVC: JSON/XML Response তৈরি করার পদ্ধতি

Step 1: Spring Boot Dependency

প্রথমে, আপনার pom.xml ফাইলে Spring Web dependency যোগ করুন যদি আপনি এটি ইতোমধ্যে না করে থাকেন।

<dependencies>
    <!-- Spring Web dependency for REST API (JSON/XML support) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    <!-- Jackson (for JSON support) -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
    </dependency>

    <!-- JAXB (for XML support) -->
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
    </dependency>
</dependencies>
  • Jackson: Spring Boot ডিফল্টভাবে JSON সাপোর্টের জন্য Jackson লাইব্রেরি ব্যবহার করে।
  • JAXB: XML রেসপন্সের জন্য JAXB (Java Architecture for XML Binding) ব্যবহার করা হয়।

Step 2: JSON Response তৈরি করা

Spring MVC-তে JSON রেসপন্স তৈরি করতে @RestController এবং @RequestMapping ব্যবহার করা হয়। @RestController অ্যানোটেশনটি @Controller এবং @ResponseBody এর সংমিশ্রণ, যা একটি JSON বা XML রেসপন্স সরাসরি রিটার্ন করতে সাহায্য করে।

Controller (JSON Response)
package com.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

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

    @GetMapping("/products")
    public List<Product> getProducts() {
        return Arrays.asList(
            new Product(1, "Laptop", 500),
            new Product(2, "Smartphone", 300)
        );
    }
}

class Product {
    private int id;
    private String name;
    private int price;

    public Product(int id, String name, int price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    // Getters and Setters
    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 int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

Explanation:

  • @RestController: এটি Spring MVC-তে একটি বিশেষ কন্ট্রোলার, যা স্বয়ংক্রিয়ভাবে রেসপন্সকে JSON ফর্ম্যাটে রিটার্ন করবে।
  • @GetMapping("/products"): HTTP GET রিকোয়েস্টে /api/products এ পৌঁছালে এটি JSON রেসপন্স হিসেবে প্রোডাক্টগুলির লিস্ট ফিরিয়ে দেবে।
  • Product Class: একটি POJO (Plain Old Java Object) যা id, name, এবং price ফিল্ড ধারণ করে। Jackson লাইব্রেরি স্বয়ংক্রিয়ভাবে এই ক্লাসের অবজেক্টকে JSON-এ রূপান্তরিত করবে।

Sample JSON Response:

[
  {
    "id": 1,
    "name": "Laptop",
    "price": 500
  },
  {
    "id": 2,
    "name": "Smartphone",
    "price": 300
  }
]

Step 3: XML Response তৈরি করা

XML রেসপন্স তৈরির জন্য JAXB এবং @XmlRootElement অ্যানোটেশন ব্যবহার করতে হবে। আপনি Spring BootJAXB বা Jackson XML ব্যবহার করতে পারেন। এখানে JAXB ব্যবহার করা হয়েছে।

Controller (XML Response)
package com.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Arrays;
import java.util.List;

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

    @GetMapping(value = "/products", produces = "application/xml")
    public List<Product> getProducts() {
        return Arrays.asList(
            new Product(1, "Laptop", 500),
            new Product(2, "Smartphone", 300)
        );
    }
}

@XmlRootElement
class Product {
    private int id;
    private String name;
    private int price;

    public Product(int id, String name, int price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }

    // Getters and Setters
    @XmlElement
    public int getId() {
        return id;
    }

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

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

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

    @XmlElement
    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

Explanation:

  • produces = "application/xml": @GetMapping এর মাধ্যমে আমরা এই রিকোয়েস্টটি XML ফর্ম্যাটে রিটার্ন করতে বলছি।
  • @XmlRootElement: JAXB অ্যানোটেশন যা Product ক্লাসকে XML রুট এলিমেন্ট হিসেবে চিহ্নিত করে।
  • @XmlElement: JAXB অ্যানোটেশন যা XML এর এলিমেন্ট হিসেবে মডেল ক্লাসের ফিল্ড গুলি চিহ্নিত করে।

Sample XML Response:

<products>
    <product>
        <id>1</id>
        <name>Laptop</name>
        <price>500</price>
    </product>
    <product>
        <id>2</id>
        <name>Smartphone</name>
        <price>300</price>
    </product>
</products>

Step 4: Handling Both JSON and XML (Content Negotiation)

Spring MVC সাপোর্ট করে Content Negotiation, যার মাধ্যমে একই এন্ডপয়েন্টের মাধ্যমে আপনি JSON এবং XML রেসপন্স উভয়ই প্রাপ্ত করতে পারেন। Spring HTTP রিকোয়েস্টের Accept হেডার চেক করে সঠিক ফরম্যাট প্রদান করবে।

Controller (Both JSON and XML)

package com.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

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

    @GetMapping("/products")
    public List<Product> getProducts() {
        return Arrays.asList(
            new Product(1, "Laptop", 500),
            new Product(2, "Smartphone", 300)
        );
    }
}

Sample Request and Responses:

  • Request for JSON: Accept: application/json
    • Response: JSON response (as shown earlier).
  • Request for XML: Accept: application/xml
    • Response: XML response (as shown earlier).

Conclusion

Spring MVC তে JSON এবং XML রেসপন্স তৈরি করা খুবই সহজ। @RestController এবং @RequestMapping ব্যবহার করে আপনি সহজে JSON রেসপন্স তৈরি করতে পারেন এবং JAXB ব্যবহার করে XML রেসপন্স তৈরি করা সম্ভব। আপনি Content Negotiation ব্যবহার করে একই এন্ডপয়েন্ট থেকে JSON এবং XML রেসপন্স উভয়ই প্রেরণ করতে পারেন, যা RESTful API ডিজাইনের একটি গুরুত্বপূর্ণ অংশ।

Content added By
Promotion

Are you sure to start over?

Loading...