Explain RequestMapping and RestController

Explain RequestMapping and RestController

1. `@RequestMapping` Annotation

The RequestMapping annotation is used to map HTTP requests to specific handler methods in your controller classes. It is a versatile annotation that can be applied to methods and classes to handle different types of HTTP requests. It allows you to specify the request URL, HTTP method (GET, POST, PUT, DELETE), and other attributes.

Explain RequestMapping and RestController

Key Purposes of `@RequestMapping`:

  • URL Mapping: Maps HTTP requests to handler methods based on the URL pattern.
  • HTTP Method Handling: Specifies the type of HTTP request (GET, POST, etc.) that a method should handle.

Request Parameters: Can handle request parameters and headers.

2. `@RestController` Annotation

The `@RestController` annotation is a specialized version of the `@Controller` annotation. It is used to create RESTful web services and controllers in Spring Boot. It combines `@Controller` and `@ResponseBody`, meaning that the data returned from each method in a `@RestController` is written directly to the HTTP response body RequestMapping and RestController.

RestController

Key Purposes of `@RestController`:

  • RESTful Services: Simplifies the creation of RESTful web services by combining `@Controller` and `@ResponseBody`.
  • Automatic JSON/XML Conversion: Automatically converts returned objects to JSON or XML based on the request’s `Accept` header.

1. @RequestMapping Annotation Example
1. `@RequestMapping` Annotation Example

Example Controller:
```java
package com.example.demo.controller;
	
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

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

    @RequestMapping(value = "/greet", method = RequestMethod.GET)
    public String greet() {
        return "Hello, World!";
    }

    @RequestMapping(value = "/hello", method = RequestMethod.POST)
    public String sayHello(String name) {
        return "Hello, " + name + "!";
    }
}
```

Explanation:

  • @RequestMapping("/api"): Maps the class-level base URL to /api.
  • @RequestMapping(value = "/greet", method = RequestMethod.GET): Maps HTTP GET requests to /api/greet to the greet() method.
  • @RequestMapping(value = "/hello", method = RequestMethod.POST): Maps HTTP POST requests to /api/hello to the sayHello() method.

Example
2. `@RestController` Annotation Example

Example Controller:
```java
package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    @GetMapping("/api/greeting")
    public String getGreeting() {
        return "Hello from GET!";
    }

    @PostMapping("/api/greeting")
    public String postGreeting(@RequestParam String name) {
        return "Hello, " + name + " from POST!";
    }
}
```

Explanation:

  • @RestController: Indicates that this class is a REST controller where each method returns a response directly to the HTTP response body.
  • @GetMapping("/api/greeting"): Simplifies @RequestMapping(method = RequestMethod.GET) for GET requests to /api/greeting.
  • @PostMapping("/api/greeting"): Simplifies @RequestMapping(method = RequestMethod.POST) for POST requests to /api/greeting.

Conclusion of RequestMapping and RestController

  • `@RequestMapping` Annotation:
  •   Purpose: Maps HTTP requests to handler methods based on URL patterns and HTTP methods.
  •   Usage: Can be applied at the class and method levels to handle various types of requests.
  • `@RestController` Annotation:
  •   Purpose: Combines `@Controller` and `@ResponseBody` to create RESTful controllers that return data directly in the response body.
  •   Usage: Simplifies the development of RESTful web services and automatic JSON/XML conversion.

Homepage

Readmore