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 ডেভেলপমেন্টে ব্যবহৃত হয়।
Read more