@Controller vs @RestController in spring boot - a details comparison

Comparison between @Controller vs @RestController in spring boot

We will look at the differences between Spring Controller and RestController in this blog.

Before that, if you don’t know what is spring boot framework please see previous blogs on Spring Boot topics.

What are controllers in spring?

In Spring, the Controller is the one responsible for handling the incoming HTTP requests and sending responses. The controller processes the request and sends the response.

To make a controller class in spring we have to annotate the class with either @Controller or @RestController.

We have written a separate blog on how to create a Spring Rest API using a controller.

Spring Controller or RestController

In spring, both @Controller and @RestController are used to create a controller to handle the HTTP request.

@Controller is used to process the incoming requests and return the view to be rendered as a response. The view can be any webpage written in HTML or JSP.

@RestController is used to handle the incoming API request, process this request, and return the response in JSON or XML format.

@RestController annotation is a combination of @Controller + @ResponseBody.


Spring RestController vs Controller in detail explanation


@Controller

In Spring MVC, the class annotated with @Controller is responsible for handling the request which is processed and returns the UI part (view) to the user.

@Controller with @ResponseBody

Since @Controller returns only the UI part if you want to have an endpoint that handles the API request inside @Controller, how can we implement it?

If you want to have a Rest API endpoint in the same @Controller class, we need to use the @ResponseBody annotation. This is because we need to mark the method as an endpoint that handles the REST calls.


@Controller
public class APIController {

	@Autowired
	private EmployeeRepository employeeRepository;

	@GetMapping("/employee")
	@ResponseBody
	public List<Employee> getEmployee() {
		return employeeRepository.findAll();
	}
}

Spring automatically converts the return value into JSON or XML and packs them into the HTTP response body if the method is annotated with @ResponseBody.

@RestController

A lot of front-end frameworks are available today for building websites and mobile apps that use APIs to get information and display it on the screen. Using Spring Boot, we can easily create an API that can be called from the frontend framework.

For example, if we have 20 Rest endpoints in the same controller, we need to add @ResponseBody to each method.

So in order to overcome this kind of situation spring introduced @RestController from spring 4.0.


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Controller
@ResponseBody
public @interface RestController {

	@AliasFor(annotation = Controller.class)
	String value() default "";

}

@RestController annotation handles the endpoint which handles API requests and returns responses in JSON or XML format.


@RestController
public class APIController {

	@Autowired
	private EmployeeRepository employeeRepository;

	@GetMapping("/employee")
	public List<Employee> getEmployee() {
		return employeeRepository.findAll();
	}
}

The @GetMapping annotation defines an endpoint that handles HTTP GET requests and the URL is “/employee” which returns all employees present in the database as a response.

Spring Boot controller and Restcontroller comparison

Post a Comment

Previous Post Next Post

Recent Posts

Facebook