What is difference between @controller and RestController?
Christopher Snyder
Updated on March 17, 2026
@Controller is used to mark classes as Spring MVC Controller. @RestController annotation is a special controller used in RESTful Web services, and it's the combination of @Controller and @ResponseBody annotation. It is a specialized version of @Component annotation.
Can we use @controller instead of @RestController?
The @Controller is a common annotation which is used to mark a class as Spring MVC Controller while the @RestController is a special controller used in RESTFul web services and the equivalent of @Controller + @ResponseBody .What is a RestController?
RestController is a Spring annotation that is used to build REST API in a declarative way. RestController annotation is applied to a class to mark it as a request handler, and Spring will do the building and provide the RESTful web service at runtime.What is difference between RestController and RequestMapping?
In case of @RestController the parameter value depicts the component name or bean name, whereas in @RequestMapping the value parameter is used to specify the path. Both are used for different purpose. If you want to specify request URI path on controller class name use @RequestMapping annotation with @RestController .What is the difference between @controller and @component?
@Component : It is a basic auto component scan annotation, it indicates annotated class is an auto scan component. @Controller : Annotated class indicates that it is a controller component, and mainly used at the presentation layer. @Service : It indicates annotated class is a Service component in the business layer.@RestController Vs @Controller. Difference between @RestController and @Controller In Spring Boot
What is difference between @service and @repository?
@Service annotation is used with classes that provide some business functionalities. @Repository Annotation is used to indicate that the class provides the mechanism for storage, retrieval, update, delete and search operation on objects. @Service Annotation is a specialization of @Component Annotation.What is difference between @bean and @component?
@Component is a class level annotation whereas @Bean is a method level annotation and name of the method serves as the bean name. @Component need not to be used with the @Configuration annotation where as @Bean annotation has to be used within the class which is annotated with @Configuration.What is the difference between PostMapping and RequestMapping?
Both do the same job. The difference is that @PostMapping is part of a predefined group of compound annotations that internally use @RequestMapping . These annotations act as shortcuts that serve to simplify the mapping of HTTP methods and to more concisely express the methods of manipulation.What is the difference between request mapping and GetMapping?
@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.What is the difference between @service and @component in Spring?
@Component is a generic stereotype for any Spring-managed component. @Service annotates classes at the service layer. @Repository annotates classes at the persistence layer, which will act as a database repository.What is the purpose of @ResponseBody?
The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object.How do you write a RestController?
jackson-dataformat-xml 2.7. 5. Maven 3. JDK 1.7.
...
Let's begin.
- Step 1: Create the directory structure. ...
- Step 2: Update pom. ...
- Step 3: Add a Pojo/domain object. ...
- Step 4: Add a Controller. ...
- Step 5: Add Configuration Class. ...
- Step 6: Add Initialization class. ...
- Step 7: Build and Deploy the application.
Is an API a controller?
Web API controller is a class which can be created under the Controllers folder or any other folder under your project's root folder. The name of a controller class must end with "Controller" and it must be derived from System. Web. Http.What is difference between Spring and spring boot?
Key DifferencesThe key difference or key feature of Spring is dependency injection and for spring boot it's autoconfiguration, with the help of Spring Boot Framework developers can reduce development time, Developer Effort, and increase productivity.
What is the difference between Spring MVC and rest?
Spring MVC Framework and RESTSpring's annotation based MVC framework simplifies the process of creating RESTful web services. The key difference between a traditional Spring MVC controller and the RESTful web service controller is the way the HTTP response body is created.