Difference RequestMapping and GetMapping

Difference RequestMapping and GetMapping

In Spring MVC, `@RequestMapping` and `@GetMapping` are annotations used to handle HTTP requests. They serve similar purposes but have different use cases and levels of specificity.

@RequestMapping:

This is a versatile annotation that can handle a wide range of HTTP methods (GET, POST, PUT, DELETE, etc.). It is used to map web requests to specific handler methods. It provides various attributes to configure the request mapping, such as `method`, `path`, `params`, and `headers`.

@GetMapping:

This is a specialized version of `@RequestMapping` introduced in Spring 4.3. It is a shorthand annotation specifically designed for handling HTTP GET requests. It simplifies the process of mapping GET requests to handler methods and is part of a set of method-specific annotations (`@PostMapping`, `@PutMapping`, `@DeleteMapping`, etc.) RequestMapping and GetMapping introduced to make code more readable and maintainable.

 RequestMapping and GetMapping


Example

Step 1: Create a Spring Boot Application

Ensure you have the necessary dependencies in your pom.xml (for Maven) or build.gradle (for Gradle).

Example
Maven Dependency:
```xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
```

Gradle Dependency:
```groovy
implementation 'org.springframework.boot:spring-boot-starter'
```

Example
Step 2: Create a Controller with `@RequestMapping`

Define a controller that handles multiple types of requests using `@RequestMapping`.

```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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
        return "Hello, " + name + "!";
    }

    @RequestMapping(value = "/goodbye", method = RequestMethod.POST)
    public String goodbye(@RequestParam(value = "name", defaultValue = "World") String name) {
        return "Goodbye, " + name + "!";
    }
}
```

Explanation:

  • @RequestMapping: Used to map HTTP requests to handler methods. The method attribute specifies the HTTP method (GET or POST).
  • @RequestParam: Used to extract query parameters from the request.

Example

Step 3: Create a Controller with `@GetMapping`

Define a controller that handles GET requests using `@GetMapping`.

```java
package com.example.demo.controller;

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

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

    @GetMapping("/greet")
    public String greet(@RequestParam(value = "name", defaultValue = "World") String name) {
        return "Greetings, " + name + "!";
    }
}
```

Explanation:

  • @GetMapping: Specifically handles GET requests, providing a more concise way to map GET requests compared to @RequestMapping.

Step 4: Running the Application
Step 4: Running the Application

Run the application from the main class (`DemoApplication`).

- Access `http://localhost:8080/api/hello?name=John` to see the response from `RequestMappingController` handling a GET request.
- Access `http://localhost:8080/api/greet?name=Jane` to see the response from `GetMappingController` handling a GET request.

Conclusion of RequestMapping and GetMapping

  • @RequestMapping: A flexible annotation that can handle different types of HTTP methods (GET, POST, PUT, DELETE) and provides more configuration options.
  • @GetMapping: A specialized shorthand annotation for handling HTTP GET requests. It simplifies the code by providing a more readable and specific mapping for GET requests.
  • Example: Demonstrated the use of @RequestMapping for both GET and POST requests and @GetMapping specifically for GET requests.

Homepage

Readmore