JSON/XML Response এবং HTTP Methods (GET, POST, PUT, DELETE)

Spring MVC এর সাথে RESTful Web Services - স্প্রিং এমভিসি (Spring MVC) - Java Technologies

256

Spring MVC একটি অত্যন্ত শক্তিশালী ফ্রেমওয়ার্ক যা ওয়েব অ্যাপ্লিকেশন এবং RESTful API তৈরি করতে ব্যবহৃত হয়। Spring MVC-তে JSON এবং XML রেসপন্স তৈরি করতে @ResponseBody অ্যানোটেশন ব্যবহার করা হয়, এবং HTTP Methods যেমন GET, POST, PUT, DELETE পরিচালনা করার জন্য @RequestMapping, @GetMapping, @PostMapping, @PutMapping, এবং @DeleteMapping অ্যানোটেশন ব্যবহার করা হয়।

এখানে আমরা JSON/XML Response এবং বিভিন্ন HTTP Methods (GET, POST, PUT, DELETE) কীভাবে ব্যবহৃত হয় তা বিস্তারিতভাবে আলোচনা করব।


1. JSON/XML Response in Spring MVC

Spring MVC তে JSON বা XML রেসপন্স ফেরত দেওয়ার জন্য @ResponseBody অ্যানোটেশন ব্যবহার করা হয়। এই অ্যানোটেশনটি Spring MVC কে বলে যে মেথডের রিটার্ন ভ্যালু সরাসরি HTTP রেসপন্স বডিতে পাঠানো হবে, এবং এটি সাধারণত JSON অথবা XML ফরম্যাটে হতে পারে।

Step 1: Spring Boot Dependencies

Spring Boot অ্যাপ্লিকেশনে JSON/XML রেসপন্স জন্য Spring Web এবং Jackson বা JAXB লাইব্রেরি ব্যবহার করা হয়।

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Jackson (JSON) এবং JAXB (XML) Spring Boot স্টার্টারের অংশ হিসেবে আসে, তবে XML ফরম্যাটে রেসপন্স দিতে JAXB এর জন্য আলাদা ডিপেনডেন্সি যুক্ত করা যেতে পারে:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-xml</artifactId>
</dependency>

Step 2: Controller with JSON/XML Response

Spring MVC তে JSON/XML রেসপন্স ফেরত দেওয়ার জন্য আপনি @ResponseBody অ্যানোটেশন ব্যবহার করবেন।

Controller Example (JSON Response)

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

@Controller
@RequestMapping("/api")
public class MyController {

    @GetMapping("/json")
    @ResponseBody
    public MyData getJsonData() {
        return new MyData("Hello", "World");
    }
}

Model Class (MyData)

public class MyData {

    private String greeting;
    private String target;

    public MyData(String greeting, String target) {
        this.greeting = greeting;
        this.target = target;
    }

    public String getGreeting() {
        return greeting;
    }

    public void setGreeting(String greeting) {
        this.greeting = greeting;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }
}
  • @ResponseBody: এই অ্যানোটেশনটি Spring MVC কে বলে যে রিটার্ন করা অবজেক্টটি সরাসরি HTTP রেসপন্স বডিতে পাঠাতে হবে এবং Spring স্বয়ংক্রিয়ভাবে Jackson লাইব্রেরি ব্যবহার করে এটি JSON এ রূপান্তরিত করবে।

এটি রিটার্ন করবে:

{
  "greeting": "Hello",
  "target": "World"
}

Controller Example (XML Response)

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

@Controller
@RequestMapping("/api")
public class MyController {

    @GetMapping("/xml")
    @ResponseBody
    public MyData getXmlData() {
        return new MyData("Hello", "World");
    }
}

XML রেসপন্সের উদাহরণ হবে:

<MyData>
    <greeting>Hello</greeting>
    <target>World</target>
</MyData>

2. HTTP Methods in Spring MVC (GET, POST, PUT, DELETE)

Spring MVC তে HTTP Methods (GET, POST, PUT, DELETE) পরিচালনার জন্য @RequestMapping অ্যানোটেশন ব্যবহার করা হয়। Spring 4 থেকে শুরু করে @GetMapping, @PostMapping, @PutMapping, @DeleteMapping অ্যানোটেশন গুলি আরও স্পষ্টভাবে নির্দিষ্ট HTTP রিকোয়েস্ট মেথডের জন্য ব্যবহৃত হয়।

GET Method Example

GET রিকোয়েস্ট ব্যবহৃত হয় ডেটা রিট্রিভ করার জন্য।

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

@Controller
@RequestMapping("/api")
public class MyController {

    @GetMapping("/greeting")
    @ResponseBody
    public String greeting() {
        return "Hello, World!";
    }
}
  • @GetMapping: এটি GET HTTP রিকোয়েস্টের জন্য ব্যবহৃত হয়। এখানে /api/greeting"Hello, World!" রিটার্ন করা হবে।

POST Method Example

POST রিকোয়েস্ট ব্যবহৃত হয় নতুন রিসোর্স তৈরি করার জন্য বা ডেটা সার্ভারে পাঠানোর জন্য।

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

@Controller
@RequestMapping("/api")
public class MyController {

    @PostMapping("/create")
    @ResponseBody
    public String createUser(@RequestBody MyData data) {
        // Data will be automatically deserialized from JSON to Java object
        return "User Created: " + data.getGreeting();
    }
}

Request Example (POST):

{
  "greeting": "Hello",
  "target": "Spring"
}
  • @PostMapping: POST রিকোয়েস্টকে হ্যান্ডেল করে এবং @RequestBody ব্যবহার করে ডেটা জটিল অবজেক্ট হিসেবে গ্রহণ করে।

PUT Method Example

PUT রিকোয়েস্ট ব্যবহৃত হয় রিসোর্স আপডেট করার জন্য।

import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

@Controller
@RequestMapping("/api")
public class MyController {

    @PutMapping("/update")
    @ResponseBody
    public String updateUser(@RequestBody MyData data) {
        return "Updated: " + data.getGreeting();
    }
}

Request Example (PUT):

{
  "greeting": "Updated Hello",
  "target": "Spring Boot"
}
  • @PutMapping: PUT রিকোয়েস্ট হ্যান্ডল করতে ব্যবহৃত হয়, এবং @RequestBody ব্যবহার করে অনুরোধের শরীরে JSON ডেটা পাঠানো হয়।

DELETE Method Example

DELETE রিকোয়েস্ট ব্যবহৃত হয় রিসোর্স মুছে ফেলার জন্য।

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.stereotype.Controller;

@Controller
@RequestMapping("/api")
public class MyController {

    @DeleteMapping("/delete")
    @ResponseBody
    public String deleteUser() {
        return "User Deleted!";
    }
}
  • @DeleteMapping: DELETE রিকোয়েস্ট হ্যান্ডল করতে ব্যবহৃত হয় এবং @ResponseBody ব্যবহার করে রেসপন্স ফর্ম্যাট করা হয়।

3. Handling JSON/XML with HTTP Methods

আপনি JSON বা XML রেসপন্সের জন্য HTTP Methods (GET, POST, PUT, DELETE) ব্যবহৃত হবে। Spring MVC স্বয়ংক্রিয়ভাবে Jackson বা JAXB লাইব্রেরি ব্যবহার করে JSON বা XML রেসপন্স তৈরি করবে।

Custom Response Headers Example

@GetMapping("/custom")
public ResponseEntity<MyData> getCustomResponse() {
    MyData data = new MyData("Hello", "Spring MVC");
    return ResponseEntity.ok()
                         .header("Custom-Header", "CustomValue")
                         .body(data);
}

এখানে, আপনি কাস্টম HTTP হেডার সহ JSON রেসপন্স ফেরত পাবেন।


Conclusion

Spring MVC তে JSON এবং XML রেসপন্স তৈরি করা এবং HTTP Methods (GET, POST, PUT, DELETE) পরিচালনা করা সহজ। @ResponseBody এবং @RequestBody অ্যানোটেশনগুলি ব্যবহার করে আপনি সহজেই JSON বা XML ডেটা গ্রহণ এবং প্রেরণ করতে পারেন, এবং Spring 4.x থেকে @GetMapping, @PostMapping, @PutMapping, @DeleteMapping অ্যানোটেশনগুলি বিভিন্ন HTTP রিকোয়েস্ট মেথডের জন্য ব্যবহৃত হয়।

Content added By
Promotion

Are you sure to start over?

Loading...