<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this article, we will explore about the RestController by using <i><b>@RestController </b></i>annotation. One of the new features of API improvements is new <i><b>@RestController</b></i> annotation which is inherited from the <i><b>@Controller</b></i> annotation.</p>
<p>Prior to the version 4.0, all the Spring MVC components has to use the common @Controller annotation to mark that as the controller servlet. When you implement a RESTful web services, the response would be always sent with the response body. To make this simple, Spring 4.0 has provided a specialized version of controller. Look at the definition of the @RestController implementation.</p>
<div id="ads-id" align="center"></div>
<pre class="highlight">@Target(value=TYPE) 
 @Retention(value=RUNTIME) 
 @Documented 
 @Controller 
 @ResponseBody 
public @interface RestController 
</pre>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<p>A convenience annotation that is itself annotated with @Controller and @ResponseBody.</p>
<p>Types that carry this annotation are treated as controllers where @RequestMapping methods assume @ResponseBody semantics by default.</p>
<p>It’s a very common use case to have Controllers implement a REST API, thus serving only JSON, XML or custom MediaType content. For convenience, instead of annotating all your @RequestMapping methods with @ResponseBody, you can annotate your Controller Class with @RestController.</p>
<h2>RestController in Spring MVC</h2>
<p><i><b>@RestController</b></i> is a stereotype annotation that combines <i><b>@ResponseBody</b></i> and <b><i>@Controller</i></b>. More than that, it gives more meaning to your Controller and also may carry additional semantics in future releases of the framework.</p>
<p>1. Download Spring MVC 4.0 jar files from this maven repository <a href="http://repo.spring.io/milestone/org/springframework/spring/4.0.0.RC1/spring-framework-4.0.0.RC1-dist.zip" target="_blank" rel="noopener">here</a>.</p>
<p>2. Create Dynamic Web Project and add the libraries you downloaded to WEB-INF/lib folder.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/02/111-3.png" border="0" /></div>
<p>3. Open /WEB-INF/web.xml file and add below configuration.</p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">; 
 <;display-name>;Spring4RestController<;/display-name>; 
 
 <;servlet>; 
 <;servlet-name>;spring4<;/servlet-name>; 
 <;servlet-class>;org.springframework.web.servlet.DispatcherServlet<;/servlet-class>; 
 <;load-on-startup>;1<;/load-on-startup>; 
 <;/servlet>; 
 <;servlet-mapping>; 
 <;servlet-name>;spring4<;/servlet-name>; 
 <;url-pattern>;/*<;/url-pattern>; 
 <;/servlet-mapping>; 
<;/web-app>; 
</pre>
<p>Note that in the above code,we have named Spring Dispatcher servlet class as &#8220;spring4&#8221; and the url pattern is given as &#8220;/*&#8221; which means any uri with the root of this web application will call DispatcherServlet. DispatcherServlet will look for configuration files following this naming convention &#8211; [servlet-name]-servlet.xml. In this example, I have named dispatcher servlet class as &#8220;spring4&#8221; and hence it will look for file named &#8216;spring4-servlet.xml&#8217;.</p>
<p>4. Now create an xml file under WEB-INF folder and name it &#8216;<b><i>spring4-servlet.xml</i></b>&#8216;.</p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">; 
 
 <;context:component-scan base-package="com.doj.spring4.controller" />; 
 
 <;mvc:annotation-driven />; 
 
<;/beans>; 
</pre>
<p>With ComponentScan tag, Spring auto scans all elements in the provided base package and all its child packages for Controller servlet. Also, we have used <;mvc:annotation-driven>; tag instead of ViewResolver, with which we can directly send response data from the controller.</p>
<p>5. Let us create a controller servlet in the package mentioned in the component-scan tag.<br />
<b>SpringRestController.java</b></p>
<pre class="highlight">package com.doj.spring4.controller; 
 
import org.springframework.web.bind.annotation.PathVariable; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.web.bind.annotation.RestController; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@RestController 
@RequestMapping("/service/greeting") 
public class SpringRestController { 
 @RequestMapping(value = "/{name}", method = RequestMethod.GET) 
 public String sayHello(@PathVariable String name) { 
 String result="Hello "+name+" to dineshonjava.com!!!"; 
 return result; 
 } 
} 
</pre>
<p>@RequestMapping&#8217; annotation is used for defining incoming request urls for class and method levels. In this case all uri&#8217;s in the format <;root-url>;/service/greeting/ will be routed to this Controller class. With @RequestMapping annotation, we can only define generic uri mappings. For dynamic uri mappings in case of passing variables along with the uri, @PathVariable is used. Here in this case, we pass a variable &#8216;name&#8217; along with the uri such as, <;root-url>;/service/greeting/Dinesh. Here the last parameter Dinesh in the uri is retrieved using @PathVariable.</p>
<p>I explained that while using <;mvc:annotation-config>; tag instead of view resolver, we use &#8216;@ResponseBody&#8217; annotation to return response data directly from controller. But in the above code, I have not used &#8216;@ResponseBody&#8217;. This is because, in Spring MVC 4.0, they have introduced &#8216;@RestController&#8217; such that we need not use &#8216;@ResponseBody&#8217; tag in each and every method. &#8216;@RestController&#8217; will handle all of that at the type level.</p>
<p>This annotation simplifies the controller and it has &#8216;@Controller&#8217; and &#8216;@ResponseBody&#8217; annotated within itself.</p>
<p>Let us take a look at the same controller but with Spring MVC 3.0,</p>
<pre class="highlight">@Controller 
@RequestMapping("/service/greeting") 
public class SpringRestController { 
 @RequestMapping(value = "/{name}", method = RequestMethod.GET) 
 public @ResponseBody String sayHello(@PathVariable String name) { 
 String result="Hello "+name+" to dineshonjava.com!!!"; 
 return result; 
 } 
} 
</pre>
<p>Note that in Spring MVC 3.0 we had to explicitly use @Controller annotation to specify controller servlet and @ResponseBody annotation in each and every method. With the introduction of &#8216;@RestController&#8217; annotation in Spring MVC 4.0, we can use it in place of @Controller and @ResponseBody annotation.</p>
<p>So now run this application in Apache Tomcat Web Server.</p>
<p><i><b>http://localhost:8080/Spring4RestController/service/greeting/Dinesh</b></i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/02/111-4.png" border="0" /></div>
<p> ;</p>
</div>
<h3><b>Download Source Code with Libs</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/Spring4RestController.zip?attredirects=0&;d=1" target="_blank" rel="noopener">Spring4@RestController.zip</a></b></h3>
<p> ;</p>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Spring MVC Related Posts</b></span></p>
<ul>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><span style="color: red;">Spring MVC Web Tutorial</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-interview-questions-and-answers/"><span style="color: red;">Spring MVC Interview Questions</span></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-mvc/"><span style="color: red;">MVC Design Pattern</span></a></b></li>
<li><b><a href="https://dineshonjava.com/what-is-dispatcherservlet-in-spring-and-its-uses/"><span style="color: red;">Spring MVC DispatcherServlet </span></a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc/"><span style="color: red;">Spring MVC WebApplicationContext and Root Application Context</span></a></b></li>
<li><b><a href="https://dineshonjava.com/stereotype-annotations-in-spring/"><span style="color: red;">Spring MVC @Controller Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestmapping-annotation-in-spring-mvc/"><span style="color: red;">Spring MVC @RequestMapping Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestparam-annotation-in-spring-mvc-with-example/"><span style="color: red;">Spring MVC @RequestParam Annotation</span></a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-applicationcontext-webapplicationcontext-in-spring-mvc/"><span style="color: red;">Spring MVC ContextLoaderListener</span></a></b></li>
<li><b><a href="https://dineshonjava.com/requestparam-vs-pathvariable-annotations-in-spring-mvc/"><span style="color: red;">Spring MVC @RequestParam and @PathVariable annotations</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-30-mvc-hello-world-example/"><span style="color: red;">Spring MVC Hello World Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-exception-handling-example/"><span style="color: red;">Spring MVC Exception Handling Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-mvc-with-hibernate-crud-example/"><span style="color: red;">Spring MVC with Hibernate CRUD Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-tiles-plugin-with-example/"><span style="color: red;">Spring MVC Tiles Plugin with Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-and-interceptor-with/"><span style="color: red;">Spring MVC Interceptor with example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/integration-spring-mvc3-and-mongodb/"><span style="color: red;">Spring MVC with MongoDB CRUD Example</span></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-3-mvc-internationalization/"><span style="color: red;">Spring MVC Internationalization &; Localization with Example</span></a></b></li>
</ul>
<p> ;</p>
</div>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/spring-4-framework-hello-world-example/">Previous</a> <;<; || <a href="https://dineshonjava.com/spring-4-tutorials-step-to-new-spring/">Index </a>|| >;>;<a href="https://dineshonjava.com/conditional-annotation-in-spring-40/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/spring-4-framework-hello-world-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/conditional-annotation-in-spring-40/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '233'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…