Spring MVC মূলত একটি ওয়েব ফ্রেমওয়ার্ক যা Model-View-Controller ডিজাইন প্যাটার্ন অনুসরণ করে। এটি প্রথাগত ওয়েব অ্যাপ্লিকেশন তৈরিতে ব্যবহৃত হয়, তবে Spring MVC ব্যবহার করে RESTful Web Services-ও তৈরি করা সম্ভব। RESTful Web Services হল একটি আর্কিটেকচার স্টাইল, যা HTTP প্রটোকল ব্যবহার করে ক্লায়েন্ট এবং সার্ভারের মধ্যে ডেটা বিনিময় করে।
Spring MVC তে RESTful Web Services তৈরি করা খুবই সহজ এবং এটি HTTP এর GET, POST, PUT, DELETE প্রমুখ মেথডগুলির মাধ্যমে রিসোর্স পরিচালনা করে। Spring MVC RESTful API তৈরি করার জন্য আমরা @RestController এবং @RequestMapping অ্যানোটেশন ব্যবহার করি।
Spring MVC এর সাথে RESTful Web Services তৈরি করার জন্য প্রয়োজনীয় পদক্ষেপ:
Step 1: Spring Boot এর জন্য ডিপেনডেন্সি যোগ করা
Spring Boot ব্যবহার করলে Spring MVC এবং RESTful Web Services এর কনফিগারেশন অত্যন্ত সহজ হয়ে যায়। প্রথমে আপনাকে spring-boot-starter-web ডিপেনডেন্সি যোগ করতে হবে।
Maven Dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Step 2: REST Controller তৈরি করা
Spring MVC-তে RESTful Web Services তৈরি করতে @RestController অ্যানোটেশন ব্যবহার করা হয়। এটি Spring MVC-কে জানায় যে, এই ক্লাসটি শুধুমাত্র JSON বা XML আকারে ডেটা রিটার্ন করবে, কোনো ভিউ তৈরি করবে না।
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/greeting")
public String greeting() {
return "Hello, Spring MVC with RESTful Web Services!";
}
}
Explanation:
@RestController: এই অ্যানোটেশন Spring MVC-কে জানায় যে, এই ক্লাসে থাকা মেথডগুলো HTTP রিকোয়েস্টের সঠিক রেসপন্স প্রদান করবে।@GetMapping("/greeting"): এটি HTTP GET রিকোয়েস্টের জন্য/greetingURL পাথকে ম্যাপ করে।
Step 3: POST, PUT, DELETE API তৈরি করা
Spring MVC তে POST, PUT, DELETE মেথড হ্যান্ডল করার জন্য @PostMapping, @PutMapping, এবং @DeleteMapping অ্যানোটেশন ব্যবহৃত হয়।
POST API উদাহরণ:
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@PostMapping("/user")
public String createUser(@RequestBody User user) {
// সাধারণভাবে ডেটাবেসে ডেটা সেভ করা হবে
return "User " + user.getName() + " created successfully!";
}
}
Explanation:
@PostMapping("/user"): HTTP POST রিকোয়েস্ট হ্যান্ডল করার জন্য এই অ্যানোটেশন ব্যবহার করা হয়েছে।@RequestBody User user: এখানে@RequestBodyব্যবহার করে JSON ফরম্যাটে পাঠানো ডেটাUserঅবজেক্টে ম্যাপ করা হচ্ছে।
User Class:
public class User {
private String name;
private int age;
// Getters and Setters
}
Example JSON Request:
{
"name": "John Doe",
"age": 30
}
Step 4: PUT API উদাহরণ
PUT API সাধারণত একটি রিসোর্স আপডেট করতে ব্যবহৃত হয়।
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@PutMapping("/user/{id}")
public String updateUser(@PathVariable int id, @RequestBody User user) {
// ডেটাবেসে user আপডেট করা হবে
return "User with ID " + id + " updated to " + user.getName();
}
}
Explanation:
@PutMapping("/user/{id}"): HTTP PUT রিকোয়েস্টের জন্য/user/{id}পাথের জন্য এটি ব্যবহার করা হয়েছে।{id}হল URL প্যারামিটার যা@PathVariableদ্বারা মডেল অবজেক্টে ম্যাপ করা হয়।@RequestBody User user: এখানে@RequestBodyব্যবহার করা হয়েছে যাতে PUT রিকোয়েস্টের মাধ্যমে JSON ডেটা সরাসরিUserঅবজেক্টে ম্যাপ করা যায়।
Step 5: DELETE API উদাহরণ
DELETE API রিসোর্স মুছে ফেলার জন্য ব্যবহৃত হয়।
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@DeleteMapping("/user/{id}")
public String deleteUser(@PathVariable int id) {
// ডেটাবেস থেকে ব্যবহারকারী মুছে ফেলা হবে
return "User with ID " + id + " deleted successfully!";
}
}
Explanation:
@DeleteMapping("/user/{id}"): HTTP DELETE রিকোয়েস্ট হ্যান্ডল করার জন্য এটি ব্যবহৃত হয়।{id}URL প্যারামিটার হিসেবে@PathVariableদ্বারা এক্সট্র্যাক্ট করা হয়।
Step 6: Spring Boot Application Class
Spring Boot ব্যবহার করে আপনার অ্যাপ্লিকেশনটি রান করার জন্য Main Application Class তৈরি করা হয়।
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestApiApplication {
public static void main(String[] args) {
SpringApplication.run(RestApiApplication.class, args);
}
}
Explanation:
@SpringBootApplication: এটি Spring Boot অ্যাপ্লিকেশন কনফিগারেশন অ্যানোটেশন যা Spring Boot অ্যাপ্লিকেশন রান করাতে ব্যবহৃত হয়।SpringApplication.run(...): Spring Boot অ্যাপ্লিকেশন চালু করার জন্য এটি ব্যবহৃত হয়।
Step 7: Application Properties (Optional)
Spring Boot অ্যাপ্লিকেশনের কনফিগারেশন করতে application.properties বা application.yml ফাইল ব্যবহার করা যায়।
Example:
server.port=8081 # Port set to 8081
Step 8: Testing RESTful Web Services
এখন আপনি Postman বা অন্য যেকোনো HTTP Client ব্যবহার করে এই RESTful API গুলো টেস্ট করতে পারবেন।
- GET Request:
- URL:
http://localhost:8080/greeting - Method:
GET - Response:
Hello, Spring MVC with RESTful Web Services!
- URL:
POST Request:
- URL:
http://localhost:8080/user - Method:
POST - Request Body (JSON):
{ "name": "Jane Doe", "age": 25 }- Response:
User Jane Doe created successfully!
- URL:
PUT Request:
- URL:
http://localhost:8080/user/1 - Method:
PUT - Request Body (JSON):
{ "name": "John Smith", "age": 30 }- Response:
User with ID 1 updated to John Smith
- URL:
- DELETE Request:
- URL:
http://localhost:8080/user/1 - Method:
DELETE - Response:
User with ID 1 deleted successfully!
- URL:
উপসংহার:
Spring MVC ব্যবহার করে RESTful Web Services তৈরি করা অত্যন্ত সহজ। @RestController, @GetMapping, @PostMapping, @PutMapping, এবং @DeleteMapping এর মাধ্যমে HTTP মেথড এবং URL পাথের সাথে মেলানো রিকোয়েস্ট হ্যান্ডল করা যায়। Spring Boot ব্যবহার করলে অ্যাপ্লিকেশন তৈরি করা অনেক সহজ হয়ে যায় এবং Spring MVC-এর সকল সুবিধা RESTful API এর জন্য উপলব্ধ হয়।
Spring MVC মূলত একটি ওয়েব ফ্রেমওয়ার্ক যা Model-View-Controller ডিজাইন প্যাটার্ন অনুসরণ করে। এটি প্রথাগত ওয়েব অ্যাপ্লিকেশন তৈরিতে ব্যবহৃত হয়, তবে Spring MVC ব্যবহার করে RESTful Web Services-ও তৈরি করা সম্ভব। RESTful Web Services হল একটি আর্কিটেকচার স্টাইল, যা HTTP প্রটোকল ব্যবহার করে ক্লায়েন্ট এবং সার্ভারের মধ্যে ডেটা বিনিময় করে।
Spring MVC তে RESTful Web Services তৈরি করা খুবই সহজ এবং এটি HTTP এর GET, POST, PUT, DELETE প্রমুখ মেথডগুলির মাধ্যমে রিসোর্স পরিচালনা করে। Spring MVC RESTful API তৈরি করার জন্য আমরা @RestController এবং @RequestMapping অ্যানোটেশন ব্যবহার করি।
Spring MVC এর সাথে RESTful Web Services তৈরি করার জন্য প্রয়োজনীয় পদক্ষেপ:
Step 1: Spring Boot এর জন্য ডিপেনডেন্সি যোগ করা
Spring Boot ব্যবহার করলে Spring MVC এবং RESTful Web Services এর কনফিগারেশন অত্যন্ত সহজ হয়ে যায়। প্রথমে আপনাকে spring-boot-starter-web ডিপেনডেন্সি যোগ করতে হবে।
Maven Dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Step 2: REST Controller তৈরি করা
Spring MVC-তে RESTful Web Services তৈরি করতে @RestController অ্যানোটেশন ব্যবহার করা হয়। এটি Spring MVC-কে জানায় যে, এই ক্লাসটি শুধুমাত্র JSON বা XML আকারে ডেটা রিটার্ন করবে, কোনো ভিউ তৈরি করবে না।
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/greeting")
public String greeting() {
return "Hello, Spring MVC with RESTful Web Services!";
}
}
Explanation:
@RestController: এই অ্যানোটেশন Spring MVC-কে জানায় যে, এই ক্লাসে থাকা মেথডগুলো HTTP রিকোয়েস্টের সঠিক রেসপন্স প্রদান করবে।@GetMapping("/greeting"): এটি HTTP GET রিকোয়েস্টের জন্য/greetingURL পাথকে ম্যাপ করে।
Step 3: POST, PUT, DELETE API তৈরি করা
Spring MVC তে POST, PUT, DELETE মেথড হ্যান্ডল করার জন্য @PostMapping, @PutMapping, এবং @DeleteMapping অ্যানোটেশন ব্যবহৃত হয়।
POST API উদাহরণ:
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@PostMapping("/user")
public String createUser(@RequestBody User user) {
// সাধারণভাবে ডেটাবেসে ডেটা সেভ করা হবে
return "User " + user.getName() + " created successfully!";
}
}
Explanation:
@PostMapping("/user"): HTTP POST রিকোয়েস্ট হ্যান্ডল করার জন্য এই অ্যানোটেশন ব্যবহার করা হয়েছে।@RequestBody User user: এখানে@RequestBodyব্যবহার করে JSON ফরম্যাটে পাঠানো ডেটাUserঅবজেক্টে ম্যাপ করা হচ্ছে।
User Class:
public class User {
private String name;
private int age;
// Getters and Setters
}
Example JSON Request:
{
"name": "John Doe",
"age": 30
}
Step 4: PUT API উদাহরণ
PUT API সাধারণত একটি রিসোর্স আপডেট করতে ব্যবহৃত হয়।
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@PutMapping("/user/{id}")
public String updateUser(@PathVariable int id, @RequestBody User user) {
// ডেটাবেসে user আপডেট করা হবে
return "User with ID " + id + " updated to " + user.getName();
}
}
Explanation:
@PutMapping("/user/{id}"): HTTP PUT রিকোয়েস্টের জন্য/user/{id}পাথের জন্য এটি ব্যবহার করা হয়েছে।{id}হল URL প্যারামিটার যা@PathVariableদ্বারা মডেল অবজেক্টে ম্যাপ করা হয়।@RequestBody User user: এখানে@RequestBodyব্যবহার করা হয়েছে যাতে PUT রিকোয়েস্টের মাধ্যমে JSON ডেটা সরাসরিUserঅবজেক্টে ম্যাপ করা যায়।
Step 5: DELETE API উদাহরণ
DELETE API রিসোর্স মুছে ফেলার জন্য ব্যবহৃত হয়।
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@DeleteMapping("/user/{id}")
public String deleteUser(@PathVariable int id) {
// ডেটাবেস থেকে ব্যবহারকারী মুছে ফেলা হবে
return "User with ID " + id + " deleted successfully!";
}
}
Explanation:
@DeleteMapping("/user/{id}"): HTTP DELETE রিকোয়েস্ট হ্যান্ডল করার জন্য এটি ব্যবহৃত হয়।{id}URL প্যারামিটার হিসেবে@PathVariableদ্বারা এক্সট্র্যাক্ট করা হয়।
Step 6: Spring Boot Application Class
Spring Boot ব্যবহার করে আপনার অ্যাপ্লিকেশনটি রান করার জন্য Main Application Class তৈরি করা হয়।
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RestApiApplication {
public static void main(String[] args) {
SpringApplication.run(RestApiApplication.class, args);
}
}
Explanation:
@SpringBootApplication: এটি Spring Boot অ্যাপ্লিকেশন কনফিগারেশন অ্যানোটেশন যা Spring Boot অ্যাপ্লিকেশন রান করাতে ব্যবহৃত হয়।SpringApplication.run(...): Spring Boot অ্যাপ্লিকেশন চালু করার জন্য এটি ব্যবহৃত হয়।
Step 7: Application Properties (Optional)
Spring Boot অ্যাপ্লিকেশনের কনফিগারেশন করতে application.properties বা application.yml ফাইল ব্যবহার করা যায়।
Example:
server.port=8081 # Port set to 8081
Step 8: Testing RESTful Web Services
এখন আপনি Postman বা অন্য যেকোনো HTTP Client ব্যবহার করে এই RESTful API গুলো টেস্ট করতে পারবেন।
- GET Request:
- URL:
http://localhost:8080/greeting - Method:
GET - Response:
Hello, Spring MVC with RESTful Web Services!
- URL:
POST Request:
- URL:
http://localhost:8080/user - Method:
POST - Request Body (JSON):
{ "name": "Jane Doe", "age": 25 }- Response:
User Jane Doe created successfully!
- URL:
PUT Request:
- URL:
http://localhost:8080/user/1 - Method:
PUT - Request Body (JSON):
{ "name": "John Smith", "age": 30 }- Response:
User with ID 1 updated to John Smith
- URL:
- DELETE Request:
- URL:
http://localhost:8080/user/1 - Method:
DELETE - Response:
User with ID 1 deleted successfully!
- URL:
উপসংহার:
Spring MVC ব্যবহার করে RESTful Web Services তৈরি করা অত্যন্ত সহজ। @RestController, @GetMapping, @PostMapping, @PutMapping, এবং @DeleteMapping এর মাধ্যমে HTTP মেথড এবং URL পাথের সাথে মেলানো রিকোয়েস্ট হ্যান্ডল করা যায়। Spring Boot ব্যবহার করলে অ্যাপ্লিকেশন তৈরি করা অনেক সহজ হয়ে যায় এবং Spring MVC-এর সকল সুবিধা RESTful API এর জন্য উপলব্ধ হয়।
Spring MVC-তে REST API তৈরি করতে @RestController এবং @RequestMapping অ্যানোটেশন ব্যবহৃত হয়। এগুলি HTTP রিকোয়েস্ট (GET, POST, PUT, DELETE ইত্যাদি) পরিচালনা করতে এবং রেসপন্স প্রদান করতে সহায়ক। @RestController অ্যানোটেশন Spring MVC তে @Controller এবং @ResponseBody অ্যানোটেশনগুলির সংমিশ্রণ হিসেবে কাজ করে। এটি RESTful ওয়েব সার্ভিস তৈরিতে সাহায্য করে, যেখানে কোনো ভিউ রেন্ডারিং না হয়ে সরাসরি JSON বা XML রেসপন্স প্রদান করা হয়।
@RestController এবং @RequestMapping এর ভূমিকা:
- @RestController: এটি একটি বিশেষ ধরনের কন্ট্রোলার, যা REST API ডেভেলপ করার জন্য ব্যবহৃত হয়। এটি @Controller এবং @ResponseBody অ্যানোটেশনগুলির সংমিশ্রণ এবং এটি প্রতিটি মেথডের রিটার্ন ভ্যালুকে HTTP Response body হিসেবে সরাসরি পাঠায়।
- @RequestMapping: এটি একটি HTTP রিকোয়েস্ট (GET, POST, PUT, DELETE) মেথডের জন্য একটি URL পাথ ম্যাপ করতে ব্যবহৃত হয়। এটি HTTP রিকোয়েস্ট এবং কন্ট্রোলারের মেথডের মধ্যে ম্যাপিং তৈরি করে।
Spring MVC তে @RestController এবং @RequestMapping ব্যবহার করে REST API তৈরি করা
Step 1: Spring Boot Project Setup
প্রথমে, Spring Boot ব্যবহার করে একটি প্রজেক্ট তৈরি করুন। Maven বা Gradle ব্যবহার করতে পারেন। নিচে Maven ডিপেন্ডেন্সি দেয়া হল:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Step 2: Create a REST Controller
@RestController ব্যবহার করে RESTful API কন্ট্রোলার তৈরি করতে হয়। নিচে একটি সাধারণ GET এবং POST রিকোয়েস্ট হ্যান্ডল করার উদাহরণ দেওয়া হলো।
Controller Example:
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;
@RestController
@RequestMapping("/api")
public class UserController {
// GET method to fetch user information
@GetMapping("/user")
public String getUser() {
return "{\"name\": \"John Doe\", \"age\": 30}";
}
// POST method to create a new user
@PostMapping("/user")
public String createUser(@RequestBody User user) {
return "{\"message\": \"User created successfully\", \"user\": " + user.toString() + "}";
}
}
- @RestController: এটি সমস্ত মেথডের রিটার্ন ভ্যালু
@ResponseBodyহিসেবে সরাসরি HTTP রেসপন্সে পাঠিয়ে দেয়। - @RequestMapping("/api"): সমস্ত রিকোয়েস্ট
/apiURL পাথের মধ্যে থাকবে। - @GetMapping("/user"): GET HTTP মেথডের জন্য
/api/userএ রিকোয়েস্ট ম্যাপ করা হয়েছে। - @PostMapping("/user"): POST HTTP মেথডের জন্য
/api/userএ রিকোয়েস্ট ম্যাপ করা হয়েছে। - @RequestBody: POST রিকোয়েস্টে পাঠানো JSON ডেটা কন্ট্রোলারে মডেল অবজেক্টে ম্যাপ করার জন্য ব্যবহৃত হয়।
Model (User) Example:
public class User {
private String name;
private int age;
// Getters and Setters
@Override
public String toString() {
return "{\"name\": \"" + name + "\", \"age\": " + age + "}";
}
}
- User ক্লাসটি একটি সিম্পল POJO (Plain Old Java Object) যা
nameএবংageফিল্ড ধারণ করে।
Step 3: Testing the API
- GET Request:
- URL:
http://localhost:8080/api/user - Method:
GET Response:
{ "name": "John Doe", "age": 30 }
- URL:
- POST Request:
- URL:
http://localhost:8080/api/user - Method:
POST Body:
{ "name": "Jane Doe", "age": 25 }Response:
{ "message": "User created successfully", "user": {"name": "Jane Doe", "age": 25} }
- URL:
Step 4: Additional HTTP Methods with @RequestMapping
@RequestMapping অ্যানোটেশন দিয়ে আপনি বিভিন্ন HTTP মেথডের জন্য রিকোয়েস্ট ম্যাপ করতে পারেন। তবে, Spring 4 থেকে @GetMapping, @PostMapping, @PutMapping, @DeleteMapping ইত্যাদি আরও স্পেসিফিক অ্যানোটেশন পাওয়া যায় যা @RequestMapping এর পরিবর্তে ব্যবহার করা যায়। নিচে এই মেথডগুলো কীভাবে ব্যবহৃত হয় তা দেখানো হল:
Example: Using @RequestMapping for Multiple Methods
@RestController
@RequestMapping("/api")
public class ProductController {
// GET method
@RequestMapping(value = "/products", method = RequestMethod.GET)
public String getAllProducts() {
return "{\"products\": [\"Product1\", \"Product2\"]}";
}
// POST method
@RequestMapping(value = "/products", method = RequestMethod.POST)
public String addProduct(@RequestBody Product product) {
return "{\"message\": \"Product added successfully\", \"product\": " + product.toString() + "}";
}
// PUT method
@RequestMapping(value = "/products/{id}", method = RequestMethod.PUT)
public String updateProduct(@PathVariable("id") int id, @RequestBody Product product) {
return "{\"message\": \"Product updated successfully\", \"product\": " + product.toString() + "}";
}
// DELETE method
@RequestMapping(value = "/products/{id}", method = RequestMethod.DELETE)
public String deleteProduct(@PathVariable("id") int id) {
return "{\"message\": \"Product deleted successfully\", \"id\": " + id + "}";
}
}
Method Annotations:
- @RequestMapping(value = "/path", method = RequestMethod.GET): HTTP GET রিকোয়েস্ট হ্যান্ডল করার জন্য।
- @RequestMapping(value = "/path", method = RequestMethod.POST): HTTP POST রিকোয়েস্ট হ্যান্ডল করার জন্য।
- @RequestMapping(value = "/path", method = RequestMethod.PUT): HTTP PUT রিকোয়েস্ট হ্যান্ডল করার জন্য।
- @RequestMapping(value = "/path", method = RequestMethod.DELETE): HTTP DELETE রিকোয়েস্ট হ্যান্ডল করার জন্য।
Step 5: Path Variables and Request Parameters
Path Variables: Spring MVC তে @PathVariable ব্যবহার করে আপনি URL পাথের অংশ হিসেবে প্যারামিটার গ্রহণ করতে পারেন।
@GetMapping("/product/{id}") public String getProduct(@PathVariable("id") int id) { return "{\"productId\": " + id + "}"; }- URL:
http://localhost:8080/api/product/1 - Response:
{"productId": 1}
- URL:
Request Parameters: @RequestParam ব্যবহার করে আপনি HTTP রিকোয়েস্টের প্যারামিটার গ্রহণ করতে পারেন।
@GetMapping("/search") public String searchProduct(@RequestParam("name") String name) { return "{\"searchResult\": \"Product name: " + name + "\"}"; }- URL:
http://localhost:8080/api/search?name=Product1 - Response:
{"searchResult": "Product name: Product1"}
- URL:
Conclusion
Spring MVC-তে @RestController এবং @RequestMapping এর মাধ্যমে RESTful API তৈরি করা অত্যন্ত সহজ এবং শক্তিশালী। @RestController ব্যবহার করে আপনি HTTP GET, POST, PUT, DELETE রিকোয়েস্ট হ্যান্ডল করতে পারেন এবং ডেটা JSON আকারে রেসপন্স হিসেবে পাঠাতে পারেন। এছাড়া, @PathVariable, @RequestParam এবং @RequestBody ব্যবহারের মাধ্যমে URL পাথের ডেটা, রিকোয়েস্ট প্যারামিটার এবং বডি ডেটা অ্যাক্সেস করা যায়। Spring MVC-তে এই ধরনের কনফিগারেশন এবং অ্যানোটেশন খুবই নমনীয় এবং কার্যকরী, যা REST API ডেভেলপমেন্টে ব্যবহৃত হয়।
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 রিকোয়েস্ট মেথডের জন্য ব্যবহৃত হয়।
RESTful Web Services হল এমন একটি সেবা (service) যা HTTP প্রটোকল এবং তার মেথডগুলি (GET, POST, PUT, DELETE) ব্যবহার করে বিভিন্ন রিসোর্সের সাথে কাজ করে। Spring MVC ফ্রেমওয়ার্ক RESTful সেবা তৈরির জন্য খুবই শক্তিশালী এবং জনপ্রিয় একটি টুল। স্প্রিং এমভিসি সহজেই @RestController এবং @RequestMapping অ্যানোটেশনগুলি ব্যবহার করে RESTful API তৈরি করতে সহায়তা করে।
এই উদাহরণে আমরা একটি সাধারণ RESTful API তৈরি করব, যা কিছু কন্ট্রোলার মেথডের মাধ্যমে GET, POST, PUT, এবং DELETE HTTP রিকোয়েস্ট প্রক্রিয়া করবে।
১. স্প্রিং এমভিসি RESTful API কনফিগারেশন
স্প্রিং এমভিসি RESTful API তৈরি করার জন্য প্রথমে স্প্রিং কনফিগারেশন সেটআপ করতে হবে। সাধারণত @EnableWebMvc এবং @ComponentScan ব্যবহার করা হয়।
WebConfig.java:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.controller")
public class WebConfig implements WebMvcConfigurer {
// You can add custom configurations here if needed
}
এখানে @EnableWebMvc স্প্রিং এমভিসি কনফিগারেশন সক্রিয় করে এবং @ComponentScan মাধ্যমে কন্ট্রোলার ক্লাসগুলো স্ক্যান করে।
২. RESTful API কন্ট্রোলার তৈরি
এখন আমরা একটি RESTful API কন্ট্রোলার তৈরি করব, যা HTTP রিকোয়েস্টের জন্য বিভিন্ন মেথড প্রক্রিয়া করবে।
UserController.java:
@RestController
@RequestMapping("/api/users")
public class UserController {
private List<User> userList = new ArrayList<>();
// Constructor to initialize some users
public UserController() {
userList.add(new User(1, "John Doe", "john@example.com"));
userList.add(new User(2, "Jane Smith", "jane@example.com"));
}
// GET: Retrieve all users
@GetMapping("/")
public List<User> getAllUsers() {
return userList;
}
// GET: Retrieve a specific user by ID
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable int id) {
for (User user : userList) {
if (user.getId() == id) {
return ResponseEntity.ok(user); // Return status 200 OK with user data
}
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); // Return 404 if not found
}
// POST: Create a new user
@PostMapping("/")
public ResponseEntity<User> createUser(@RequestBody User user) {
user.setId(userList.size() + 1);
userList.add(user);
return ResponseEntity.status(HttpStatus.CREATED).body(user); // Return status 201 Created
}
// PUT: Update a user by ID
@PutMapping("/{id}")
public ResponseEntity<User> updateUser(@PathVariable int id, @RequestBody User user) {
for (User existingUser : userList) {
if (existingUser.getId() == id) {
existingUser.setName(user.getName());
existingUser.setEmail(user.getEmail());
return ResponseEntity.ok(existingUser); // Return updated user with status 200 OK
}
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); // Return 404 if user not found
}
// DELETE: Delete a user by ID
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable int id) {
Iterator<User> iterator = userList.iterator();
while (iterator.hasNext()) {
User user = iterator.next();
if (user.getId() == id) {
iterator.remove(); // Remove the user from the list
return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); // Return 204 No Content
}
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); // Return 404 if user not found
}
}
কন্ট্রোলার ব্যাখ্যা:
@RestController: এই অ্যানোটেশনটি স্প্রিংকে নির্দেশ করে যে এটি একটি RESTful কন্ট্রোলার ক্লাস এবং@ResponseBodyঅ্যানোটেশনের মতো প্রতিটি রিটার্ন ভ্যালুকে সরাসরি HTTP রেসপন্সে পাঠাবে।@RequestMapping("/api/users"): এই অ্যানোটেশনটি API রুট নির্ধারণ করে (এখানে/api/usersURL পাথ)।@GetMapping("/"): GET HTTP রিকোয়েস্টের জন্য ব্যবহার করা হয়। এটি সমস্ত ইউজারের তালিকা রিটার্ন করে।@GetMapping("/{id}"): একটি নির্দিষ্ট User আইডি অনুসারে ইউজারের ডাটা রিটার্ন করে।@PostMapping("/"): POST HTTP রিকোয়েস্টের মাধ্যমে একটি নতুন ইউজার তৈরি করা হয়।@PutMapping("/{id}"): PUT HTTP রিকোয়েস্টের মাধ্যমে একটি নির্দিষ্ট ইউজারের ডাটা আপডেট করা হয়।@DeleteMapping("/{id}"): DELETE HTTP রিকোয়েস্টের মাধ্যমে একটি ইউজার ডিলিট করা হয়।
User.java:
এখানে একটি সাধারণ User ক্লাস তৈরি করা হলো যা আমাদের ডাটা রিপ্রেজেন্ট করবে।
public class User {
private int id;
private String name;
private String email;
// Constructors, Getters, Setters
public User(int id, String name, String email) {
this.id = id;
this.name = name;
this.email = email;
}
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;
}
}
৩. Application Configuration (Main Class)
স্প্রিং বুট অ্যাপ্লিকেশন শুরু করার জন্য একটি মূল ক্লাস তৈরি করতে হবে। এটি @SpringBootApplication অ্যানোটেশন দিয়ে চিহ্নিত করা হবে।
@SpringBootApplication
public class SpringMvcRestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMvcRestApplication.class, args);
}
}
৪. API Test:
এখন, আমরা আমাদের RESTful API-টি Postman বা অন্য কোনো HTTP ক্লায়েন্ট ব্যবহার করে পরীক্ষা করতে পারি।
- GET /api/users/: সমস্ত ইউজারের তালিকা দেখতে।
- GET /api/users/{id}: নির্দিষ্ট ইউজার (যেমন,
GET /api/users/1) দেখতে। - POST /api/users/: নতুন ইউজার তৈরি করতে:
- Request Body:
{"name": "Alex", "email": "alex@example.com"}
- Request Body:
- PUT /api/users/{id}: ইউজারের তথ্য আপডেট করতে:
- Request Body:
{"name": "Alex Updated", "email": "alex.updated@example.com"}
- Request Body:
- DELETE /api/users/{id}: ইউজার মুছে ফেলতে (যেমন,
DELETE /api/users/1)
উপসংহার:
- Spring MVC ফ্রেমওয়ার্ক RESTful ওয়েব সার্ভিস তৈরি করার জন্য অত্যন্ত শক্তিশালী এবং সহজ উপায় প্রদান করে।
@RestController,@RequestMapping,@GetMapping,@PostMapping,@PutMapping, এবং@DeleteMappingএর মতো অ্যানোটেশনগুলি RESTful API তৈরি করতে সাহায্য করে।- এই উদাহরণে আমরা দেখেছি কিভাবে বিভিন্ন HTTP মেথড ব্যবহার করে CRUD অপারেশন (Create, Read, Update, Delete) সম্পন্ন করা যায়।
Read more