Spring Boot Interview Questions
Spring Boot
Java
BackendWeb DevelopmentQuestion 11
What are the @RestController
and @RequestMapping
annotations used for?
Answer:
The @RestController
and @RequestMapping
annotations in Spring Boot are used to build RESTful web services. They simplify the process of mapping HTTP requests to handler methods in your controller classes.
@RestController
- Purpose: The
@RestController
annotation is a specialized version of the@Controller
annotation. It is used to create RESTful web services by combining@Controller
and@ResponseBody
. - Functionality:
- Marks the class as a controller where every method returns a domain object instead of a view.
- All methods in a class annotated with
@RestController
will have their return values serialized into the HTTP response body.
- Example:
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
@RestController
public class MyRestController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, World!";
}
}
In this example, the sayHello
method returns a string that will be written directly to the HTTP response body.
@RequestMapping
- Purpose: The
@RequestMapping
annotation is used to map HTTP requests to handler methods of MVC and REST controllers. - Functionality:
- Can be applied at the class level and method level.
- Defines the URL patterns and HTTP methods that a particular handler method or class should process.
- Attributes:
value
orpath
: The URL pattern to which the handler method is mapped.method
: The HTTP method (GET, POST, PUT, DELETE, etc.) that the handler method should support.params
,headers
,consumes
,produces
, etc.: Additional constraints for the mapping.
- Example:
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 MyApiController {
@RequestMapping(value = "/greet", method = RequestMethod.GET)
public String greet() {
return "Greetings from the API!";
}
}
In this example, the greet
method is mapped to handle GET requests to /api/greet
.
Simplified Annotations for Specific HTTP Methods
Spring Boot also provides shortcut annotations for common HTTP methods, which are more concise than using @RequestMapping
with the method
attribute.
- @GetMapping: Shortcut for
@RequestMapping(method = RequestMethod.GET)
- @PostMapping: Shortcut for
@RequestMapping(method = RequestMethod.POST)
- @PutMapping: Shortcut for
@RequestMapping(method = RequestMethod.PUT)
- @DeleteMapping: Shortcut for
@RequestMapping(method = RequestMethod.DELETE)
Example Using Shortcut Annotations
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class MyApiController {
@GetMapping("/greet")
public String greet() {
return "Greetings from the API!";
}
@PostMapping("/create")
public String create() {
return "Create a new resource";
}
@PutMapping("/update")
public String update() {
return "Update an existing resource";
}
@DeleteMapping("/delete")
public String delete() {
return "Delete a resource";
}
}
Combining @RestController and @RequestMapping
You can combine @RestController
with @RequestMapping
at the class level to apply a common base path to all methods within the controller.
Example
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@RestController
@RequestMapping("/api")
public class MyApiController {
@GetMapping("/greet")
public String greet() {
return "Greetings from the API!";
}
@PostMapping("/create")
public String create() {
return "Create a new resource";
}
}
In this example, the base URL /api
is applied to all methods in the MyApiController
class, making the endpoints /api/greet
and /api/create
.
Summary
- @RestController:
- Combines
@Controller
and@ResponseBody
. - Marks the class as a RESTful controller where every method returns data directly written to the HTTP response body.
- Combines
- @RequestMapping:
- Maps HTTP requests to handler methods.
- Can define URL patterns, HTTP methods, and other request constraints.
- Can be used at both class and method levels.
- Shortcut Annotations:
@GetMapping
,@PostMapping
,@PutMapping
,@DeleteMapping
provide more concise ways to map specific HTTP methods.
Using @RestController
and @RequestMapping
(or its shortcuts) effectively allows you to build powerful and efficient RESTful web services in Spring Boot.