Difference between @Controller vs @RestController in Spring
Tags: programming, rest, spring, java
Published: 2023-11-25 10:30:46

---
title: Difference between @Controller vs @RestController in Spring
published: true
description: Difference between @Controller vs @RestController in Spring
tags: programming, REST, spring, java
# cover_image: https://direct_url_to_image.jpg
# Use a ratio of 100:42 for best results.
# published_at: 2023-11-25 10:26 +0000
---
Hello folks, if you are preparing for Java and Spring Developer interview then you may have come across *"difference between @Controller and @RestController annotation"* in Spring Boot and Spring Framework in general. It's one of the popular spring question which I have also mentioned in my list of [**25 Spring Framework questions earlier**](https://medium.com/javarevisited/25-spring-framework-interview-questions-for-1-to-3-years-experienced-java-programmers-567f268ed897)

In Spring MVC, both `@Controller` and `@RestController` annotations are used to define web controllers as per MVC Design pattern. A controller is responsible for handling HTTP request and returning HTTP response to client.

> The main difference between these two annotation is how they handle client's request and when they are used

For example [*@Controller* annotation](https://javarevisited.blogspot.com/2017/11/difference-between-component-service.html) is there from day 1 and it is used to mark a class as a web controller to process HTTP requests and return a view name, which is then resolved by a view resolver to generate the final HTML view.

On the other hand `@RestController` annotation was added in later Spring version like Spring 3.4 to increase support for REST API development. In case of REST API, instead of returning HTML, you will probably like to return JSON or XML as your client is no more human but machine.

If you want to return a JSON or XML from a Spring MVC controller then you need to add @ReseponseBody annotation to each of Controller method and it seems overkill while implementing REST APIs using Spring and Spring Boot.

Designers and developers of Spring Framework recognized this shortcoming and then added a new annotation called **@RestController** in Spring 3.4 version.

The `@RestController` annotation is a combination of the `@Controller` and `@ResponseBody` annotations and you can use it to implement REST APIs in Java and Spring Boot.

> The key difference between `@Controler` and `@RestController` annotation is @ResponseBody annotation, `@``Controler` does not automatically add the `@ResponseBody` annotation to all of the controller's methods, which means that you need to add it to each method individually if you want to return a JSON or XML response.`@RestController` automatically adds the `@ResponseBody` annotation to all of the controller's methods.

In the past, I have shared many questions for Java developer interviews like these [**35 Java Interview Questions**](https://medium.com/javarevisited/top-10-java-interview-questions-for-3-to-4-years-experienced-programmers-c4bf6d8b5e7b), [**Advanced Java Interview Questions**](https://medium.com/javarevisited/20-advanced-core-java-interview-questions-for-experienced-developers-of-5-to-10-years-87b243276134),[ **Spring Framework Interview Questions**](https://medium.com/javarevisited/25-spring-framework-interview-questions-for-1-to-3-years-experienced-java-programmers-567f268ed897) and [**System Design Questions**](https://medium.com/javarevisited/7-system-design-problems-to-crack-software-engineering-interviews-in-2023-13a518467c3e) and today, I have answered one of the popular spring question, if you are preparing for interviews, you can also check out those articles.

*Now let's see examples of both @Controller and @RestController annotation in Spring framework*



## @Controller and @RestController Example in Spring Boot
Here is a simple example of `@Controller` annotation which is used to return a response for simple HTTP request on `"/hello"` path:

```java
@Controller
public class MyController {

    @Autowired\
    private MyService myService;

    @RequestMapping("/hello")\
    public String sayHello(Model model) {\
        model.addAttribute("message", myService.getHelloMessage());\
        return "hello";\
    }\
}

```

In this example, the `MyController` class is annotated with `@Controller` annotation and has a single method, `sayHello()`, which is mapped to the `/hello` URL.

The method takes a `Model` object as a parameter and adds an attribute called `"message"` to it. The method then returns a string "hello", which is the name of the view to be rendered.

Now, let's see an example of `@RestController` annotation from Spring Framework, which i used to respond REST API

``` java
@RestController\
public class MyRestController {

    @Autowired\
    private MyService myService;

    @RequestMapping("/greeting")\
    public Greeting getGreeting() {\
        return myService.getGreeting();\
    }\
}
```

In this example, the `MyRestController` class is annotated with `@RestController` and has a single method, `getGreeting()`, which is mapped to the `/greeting` URL.

The method returns an object of the `Greeting` class, which will be **automatically converted to a JSON or XML representation** and sent as the response to the client.

It is also worth noting that the `Greeting` class should have `getters` and `setters` for the data that you want to be included in the response, otherwise, it will not be serialized properly or converted to JSON.

### @Controller and @RestController annotation in Spring Boot

Here are a few things to keep in mind while using the `@Controller` and `@RestController` annotations:

-   When using the @Controller annotation, you will need to use the [@ResponseBody annotation](https://www.java67.com/2022/02/what-is-responsebody-and-requestbody-annotations-in-spring.html) to return JSON or XML data, otherwise, the response will be treated as a view name and a view resolver will try to resolve it while @ResponseBody annotation is not necessary as it is already included in the annotation.
-   When using the @RestController annotation, you can also use the [*@ResponseStatus annotation to set the HTTP status code of the response* ](https://javarevisited.blogspot.com/2021/09/how-to-return-different-http-status-from-sprnig-mvc-controller.html)and @RequestBody annotation to bind the request body to a method parameter.
-   And while using the @RestController annotation, you can use the [@RequestMapping annotation](https://javarevisited.blogspot.com/2021/10/difference-between-requestmapping-and..html) to set the URL path to map the methods to.
-   Another thing which is worth noting is that, If you are returning a complex object that contains child objects, make sure all the child object classes have getters and setters, otherwise, they will not be serialized properly.

That's all about the *difference between @Controller and @RestController annotation in Spring Boot.* Just remember that @Controller is used to create web controllers that return views, which is further resolved by view resolver, while `@RestController` is used to create web services that return JSON or XML data. It's also worth noting.