Examples of @GetMapping annotation
1. Basic Example of @GetMapping
The simplest use case of @GetMapping is returning a string response when a user accesses a specific endpoint.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Welcome to Spring Boot!";
}
}
Explanation:
- @RestController marks this class as a REST controller.
- @RequestMapping("/api") sets a base path for all endpoints in this controller.
- @GetMapping("/hello") maps the /hello endpoint to the sayHello method.
- When accessed via http://localhost:8080/api/hello, it returns Hello, Welcome to Spring Boot!.
2. @GetMapping with Path Variable
Sometimes, we need to pass values through the URL and use them in our application. This can be done using Path Variables.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/user/{name}")
public String greetUser(@PathVariable String name) {
return "Hello, " + name + "! Welcome to Spring Boot.";
}
}
Explanation:
- @PathVariable extracts the value from the URL and passes it as a method parameter.
- If we access http://localhost:8080/api/user/Alex, it returns Hello, Alex! Welcome to Spring Boot.
3. @GetMapping with Request Parameters
Sometimes, we want to accept query parameters instead of path variables. We use @RequestParam for this.
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 MyController {
@GetMapping("/welcome")
public String welcomeUser(@RequestParam String name, @RequestParam int age) {
return "Welcome " + name + "! You are " + age + " years old.";
}
}
Explanation:
- @RequestParam extracts values from the URL’s query parameters.
- If we access http://localhost:8080/api/welcome?name=John&age=25, it returns Welcome John! You are 25 years old.
4. @GetMapping Returning JSON Response
A common use case of @GetMapping is returning JSON responses instead of plain text.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Arrays;
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/students")
public List<String> getStudents() {
return Arrays.asList("Alice", "Bob", "Charlie");
}
}
Explanation:
- The method returns a list of student names as JSON.
- When accessed at http://localhost:8080/api/students, the output is:
["Alice", "Bob", "Charlie"]
5. @GetMapping with Custom Response Type
Sometimes, we need to return a complex object as a response. This is possible by creating a Java class and returning it as JSON.
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
class Student {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
}
@RestController
@RequestMapping("/api")
public class MyController {
@GetMapping("/student")
public Student getStudent() {
return new Student("David", 22);
}
}
Explanation:
- We create a Student class with name and age attributes.
- The method getStudent() returns a new Student object.
- When accessed at http://localhost:8080/api/student, the output is:
{
"name": "David",
"age": 22
}
Frequently Asked Questions
What is the purpose of @GetMapping in Spring Boot?
@GetMapping is used to map HTTP GET requests to specific handler methods inside a controller. It simplifies handling GET requests in a REST API.
How is @GetMapping different from @RequestMapping?
@GetMapping is a specialized version of @RequestMapping(method = RequestMethod.GET), making the code more readable and concise.
Can @GetMapping return JSON responses?
Yes, @GetMapping can return JSON responses when the method returns an object or list. Spring Boot automatically converts the response into JSON format.
Conclusion
In this article, we learned the @GetMapping annotation in Spring Boot, which is used to handle HTTP GET requests in RESTful web services. It simplifies request mapping by making the code more readable and efficient. Understanding @GetMapping helps developers build well-structured APIs that retrieve data easily, improving the performance and maintainability of web applications.