উদাহরণ সহ বিভিন্ন HTTP Methods এর ব্যবহার

HTTP Methods এবং Request Handling - স্প্রিং বুট ক্লায়েন্ট (Spring Boot Client) - Java Technologies

392

স্প্রিং বুটে RestTemplate বা WebClient ব্যবহার করে বিভিন্ন HTTP মেথড যেমন GET, POST, PUT, DELETE, ইত্যাদির মাধ্যমে RESTful API-তে অনুরোধ করা যায়। নিচে উদাহরণসহ এই মেথডগুলোর ব্যবহার দেখানো হলো:


GET Method

GET মেথড সাধারণত সার্ভার থেকে ডেটা সংগ্রহের জন্য ব্যবহৃত হয়।

উদাহরণ: RestTemplate ব্যবহার করে

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class RestTemplateClient {

    private final RestTemplate restTemplate;

    public RestTemplateClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String getExample(String url) {
        return restTemplate.getForObject(url, String.class);
    }
}

কল করার উদাহরণ:

String response = restTemplateClient.getExample("https://jsonplaceholder.typicode.com/posts/1");
System.out.println(response);

POST Method

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

উদাহরণ: RestTemplate ব্যবহার করে

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class PostClient {

    private final RestTemplate restTemplate;

    public PostClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public String createPost(String url, Object requestBody) {
        ResponseEntity<String> response = restTemplate.postForEntity(url, requestBody, String.class);
        return response.getBody();
    }
}

কল করার উদাহরণ:

Map<String, String> requestBody = new HashMap<>();
requestBody.put("title", "foo");
requestBody.put("body", "bar");
requestBody.put("userId", "1");

String response = postClient.createPost("https://jsonplaceholder.typicode.com/posts", requestBody);
System.out.println(response);

PUT Method

PUT মেথড ব্যবহার করে বিদ্যমান ডেটা আপডেট করা হয়।

উদাহরণ: RestTemplate ব্যবহার করে

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class PutClient {

    private final RestTemplate restTemplate;

    public PutClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public void updatePost(String url, Object requestBody) {
        restTemplate.put(url, requestBody);
    }
}

কল করার উদাহরণ:

Map<String, String> requestBody = new HashMap<>();
requestBody.put("id", "1");
requestBody.put("title", "updated title");
requestBody.put("body", "updated body");
requestBody.put("userId", "1");

putClient.updatePost("https://jsonplaceholder.typicode.com/posts/1", requestBody);

DELETE Method

DELETE মেথড ব্যবহার করে ডেটা মুছে ফেলা হয়।

উদাহরণ: RestTemplate ব্যবহার করে

import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class DeleteClient {

    private final RestTemplate restTemplate;

    public DeleteClient(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
    }

    public void deletePost(String url) {
        restTemplate.delete(url);
    }
}

কল করার উদাহরণ:

deleteClient.deletePost("https://jsonplaceholder.typicode.com/posts/1");

WebClient ব্যবহার করে সব HTTP মেথড

WebClient রিয়াক্টিভ প্রোগ্রামিংয়ের জন্য ব্যবহৃত হয়। এটি RestTemplate-এর বিকল্প।

উদাহরণ: GET, POST, PUT, DELETE

import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

@Service
public class WebClientExample {

    private final WebClient webClient;

    public WebClientExample(WebClient.Builder webClientBuilder) {
        this.webClient = webClientBuilder.baseUrl("https://jsonplaceholder.typicode.com").build();
    }

    // GET Method
    public String getPost(int id) {
        return webClient.get()
                        .uri("/posts/{id}", id)
                        .retrieve()
                        .bodyToMono(String.class)
                        .block();
    }

    // POST Method
    public String createPost(Object requestBody) {
        return webClient.post()
                        .uri("/posts")
                        .bodyValue(requestBody)
                        .retrieve()
                        .bodyToMono(String.class)
                        .block();
    }

    // PUT Method
    public void updatePost(int id, Object requestBody) {
        webClient.put()
                 .uri("/posts/{id}", id)
                 .bodyValue(requestBody)
                 .retrieve()
                 .toBodilessEntity()
                 .block();
    }

    // DELETE Method
    public void deletePost(int id) {
        webClient.delete()
                 .uri("/posts/{id}", id)
                 .retrieve()
                 .toBodilessEntity()
                 .block();
    }
}

কল করার উদাহরণ:

// GET
String getResponse = webClientExample.getPost(1);
System.out.println(getResponse);

// POST
Map<String, String> requestBody = new HashMap<>();
requestBody.put("title", "foo");
requestBody.put("body", "bar");
requestBody.put("userId", "1");
String postResponse = webClientExample.createPost(requestBody);
System.out.println(postResponse);

// PUT
requestBody.put("title", "updated title");
webClientExample.updatePost(1, requestBody);

// DELETE
webClientExample.deletePost(1);

সারাংশ:

RestTemplate মেথড:

  1. GET: getForObject(), getForEntity()
  2. POST: postForObject(), postForEntity()
  3. PUT: put()
  4. DELETE: delete()

WebClient মেথড:

  1. GET: get()
  2. POST: post()
  3. PUT: put()
  4. DELETE: delete()

উপরের উদাহরণগুলো অনুসরণ করে আপনি স্প্রিং বুট ক্লায়েন্ট দিয়ে যেকোন HTTP মেথড ব্যবহার করতে পারবেন।

Content added By
Promotion

Are you sure to start over?

Loading...