DEV Community

Tamilarasi Nathan
Tamilarasi Nathan

Posted on

RestController

I am creating a Java class and defining some methods that contain business logic. This class is annotated with @RestController, which makes it a REST controller in a Spring Boot application.
Instead of using @Controller, I use @RestController because it automatically returns the response in JSON or XML format (depending on the client request and configuration)
I give a unique base path to this controller using:
@RequestMapping(value = "/vowelsController")

Inside the class, I define methods and map them to specific HTTP requests. For example, I map a method that converts a name to uppercase using:

@GetMapping("/uppercase/{name}")

When an HTTP GET request is made to this endpoint (e.g., /vowelsController/uppercase/John), the method executes its logic and returns a response.
Since this is a REST controller, the response is automatically returned in JSON (or XML) format, which can be consumed by a frontend application. Based on the returned data, we can design the user interface to display the output.
When ever MVC (returns views like HTML)that we can use @Controller
But REST APIs (returns data: JSON/XML) that we can use only @RestController

Top comments (0)