title: @RestController VS @Controller in Spring MVC with AngularJs
body: <p>I want to create an application with Spring MVC Rest Web services and angularJs.
  So my probleme is when i put in my controller @Controller everythings works (I have my page home1.jsp) but when i use @RestController my browser display a String ("home1").
  Can you please help to understand my probleme, Thanks.
  Here is my code:
  *AppController</p>
  
  <pre><code>@RestController
  @RequestMapping("/service")
  public class AppController {
  
      @Autowired
      MyService myService;
  
      private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
  
      /**
       * Simply selects the home view to render by returning its name.
       */
      @RequestMapping(value = "/home1", method = RequestMethod.GET)
      public String home(Locale locale, Model model) {
          logger.info("Welcome home! The client locale is {}.", locale);
  
          Date date = new Date();
          DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
  
          String formattedDate = dateFormat.format(date);
  
          model.addAttribute("serverTime", formattedDate );
  
          return "home1";
      }
  
      // GET ALL
      @RequestMapping(value="/allProfiles",method = RequestMethod.GET,headers="Accept=application/json")
      public List&lt;Profil&gt; getProfils()
      {
          System.out.println("RestControllern methode initialized");
          return myService.getAllProfiles();
      }
  
      @RequestMapping(value="/allPatterns",method = RequestMethod.GET,headers="Accept=application/json")
      public @ResponseBody List&lt;Pattern&gt; getPatterns()
      {
          logger.info("Welcome home! allPatterns {}.");
          System.out.println("RestControllern methode initialized");
          Pattern p = new Pattern();
          p.setId(5);
          p.setValue("moiuuyyt");
          return myService.getAllPattern();
  
      }
  
      @RequestMapping(value="/allComponents",method = RequestMethod.GET,headers="Accept=application/json")
      public List&lt;Component&gt; getComponents()
      {
          return myService.getAllComponent();
      }
  
      //GET ONE
      @RequestMapping(value="/components/{id}",method = RequestMethod.GET,headers="Accept=application/json")
      public Component getComponent(@PathVariable int id)
      {
          return myService.getComponentById(id);
      }
  
      @RequestMapping(value="/profiles/{id}",method = RequestMethod.GET,headers="Accept=application/json")
      public Profil getProfil(@PathVariable int id)
      {
          return myService.getProfilById(id);
      }
  
      @RequestMapping(value="/patterns/{id}",method = RequestMethod.GET,headers="Accept=application/json")
      public Pattern getPattern(@PathVariable int id)
      {
          return myService.getPatternById(id);
      }
  
      //SAVE
      @RequestMapping(value="/addpattern/{value}",method= RequestMethod.POST,  headers="Accept=application/json")
      public String addPattern(@PathVariable String value)
      {
          Pattern pat = new Pattern();
          pat.setValue(value);
          myService.addPattern(pat);
          return "redirect:/service/allPatterns";
      }
  
      @RequestMapping(value="/addprofil",method = RequestMethod.POST,headers="Accept=application/json")
      public void addProfil()
      {
          Profil pr = new Profil();
          pr.setProfilName("Profil1");
          myService.addProfil(pr);
      }
  
      @RequestMapping(value="/addcomponent",method = RequestMethod.POST,headers="Accept=application/json")
      public void  addComponent(Component cp)
      {
  
          myService.addComponent(cp);
      }
  
      public MyService getMyService() {
          return myService;
      }
  
      public void setMyService(MyService myService) {
          this.myService = myService;
      }
  
  
      /**
       * @return The page of all Patterns after we have updated one of them to test the method
       */
      @RequestMapping(value="/updatepattern/{i}/{value}",method= RequestMethod.PUT,  headers="Accept=application/json")
      public String updatePattern(@PathVariable int i,@PathVariable String value)
      {
          /*List&lt;Pattern&gt; liste = myService.getAllPattern();
          Pattern p = liste.get(0);       
          p.setValue("^/[0-9]Pattern has been updated");
          myService.updatePattern(p);*/
          Pattern p  = myService.getPatternById(i);
          p.setValue(value);
          myService.updatePattern(p);
          return "redirect:/service/home1";
      }   
  
  
      /**
       * @return The page of all Patterns after we have deleted one of them to test the method
       */
      @RequestMapping(value="/deletepattern/{id}",method= RequestMethod.DELETE,  headers="Accept=application/json")
      public String DeletePattern(@PathVariable int id)
      {
          /*List&lt;Pattern&gt; liste = myService.getAllPattern();
          Pattern p = liste.get(3);*/
          Pattern p  = myService.getPatternById(id);
          myService.deletePattern(p);
          return "redirect:/service/home1";
      }   
  
  }
  </code></pre>
  
  <p>***Home1.jsp</p>
  
  <pre><code>&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  &lt;%@ page session="false" %&gt;
  &lt;!Doctype html&gt;
  &lt;html data-ng-app="myTest" lang="en"&gt;
      &lt;head&gt;
          &lt;title&gt;Home&lt;/title&gt;
          &lt;script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"&gt;&lt;/script&gt;
          &lt;script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js"&gt;&lt;/script&gt;
          &lt;script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-resource.min.js"&gt;&lt;/script&gt;
          &lt;script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"&gt;&lt;/script&gt;
          &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
          &lt;link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"&gt;
          &lt;meta charset="utf-8"&gt;
          &lt;meta name="viewport" content="width=device-width, initial-scale=1"&gt;
  
  
  
      &lt;/head&gt;
      &lt;body &gt;
          &lt;h1&gt;
              Hello world!  
          &lt;/h1&gt;
  
          &lt;P&gt;  The time on the server is ${serverTime}. &lt;/P&gt;
          &lt;hr/&gt;
          &lt;div data-ng-controller="myController" style="margin-left: 100px"&gt;
              &lt;div&gt;
                  &lt;p&gt;ALL PATTERNS&lt;/p&gt;
                  &lt;button class="btn btn-default" data-ng-click = "listPattern()"&gt;All Patterns&lt;/button&gt;               
                  &lt;br/&gt;&lt;br/&gt;
                  &lt;table class="table" data-ng-hide="state" style="width: 600px"&gt;             
                      &lt;tr&gt;
                          &lt;th&gt;Id_Pattern&lt;/th&gt;
                          &lt;th&gt;Pattern&lt;/th&gt;
                      &lt;/tr&gt;
                      &lt;tr data-ng-repeat="p in patterns"&gt;
                          &lt;td&gt;{{p.id}}&lt;/td&gt;
                          &lt;td&gt;{{p.value}}&lt;/td&gt;
                      &lt;/tr&gt;               
                  &lt;/table&gt;                
              &lt;/div&gt;
              &lt;br/&gt;&lt;br/&gt;&lt;hr/&gt;
              &lt;div&gt;
                  &lt;h3&gt;ADD PATTERN&lt;/h3&gt;
                  &lt;form class="form-inline"&gt;
                      &lt;div class="form-group"&gt;
                          &lt;input  type ="text" class="form-control" placeholder="Pattern value" data-ng-model="value"/&gt; 
                          &lt;button class="btn btn-default" data-ng-click = "addPattern()"&gt;New Pattern&lt;/button&gt; 
                      &lt;/div&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
              &lt;br/&gt;&lt;br/&gt;&lt;hr/&gt;
              &lt;div&gt;
                  &lt;h3&gt;UPDATE PATTERN&lt;/h3&gt;
                  &lt;form class="form-inline"&gt;
                      &lt;div class="form-group"&gt;
                          &lt;input  type ="text" class="form-control" placeholder="PATTERN_ID" data-ng-model="selectedPattern"/&gt; 
                          &lt;input  type ="text" class="form-control" placeholder="new Pattern value" data-ng-model="newvalue"/&gt; 
                          &lt;button class="btn btn-default" data-ng-click = "updatePattern()"&gt;Update&lt;/button&gt; 
                      &lt;/div&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
              &lt;br/&gt;&lt;br/&gt;&lt;hr/&gt;
              &lt;div&gt;
                  &lt;h3&gt;DELETE PATTERN&lt;/h3&gt;
                  &lt;form class="form-inline"&gt;
                      &lt;div class="form-group"&gt;
                          &lt;input  type ="text" class="form-control" placeholder="PATTERN ID" data-ng-model="id_pattern"/&gt; 
                          &lt;button class="btn btn-default" data-ng-click = "deletePattern()"&gt;Delete&lt;/button&gt; 
                      &lt;/div&gt;
                  &lt;/form&gt;
              &lt;/div&gt;
  
          &lt;/div&gt;
  
  
          &lt;script&gt;
              var module = angular.module('myTest',[]);
              module.controller('myController',function($scope,$http){
  
                  //function to get Patterns Lists
                  $scope.state = true;
                  $scope.listPattern = function() {           
                      $http({method: 'GET', url: 'allPatterns'}).
                      success(function(data) {
                          $scope.patterns = data;
                          $scope.state = !$scope.state;
                      });
                  };
  
  
                  /*Function to add pattern
                  @param the pattern value
                  @return pattern lists
                  */
                  $scope.addPattern = function(){
                      if($scope.value == "" )
                      {
                          alert("You must Enter all specified fields");
                      }
                      else
                      {
                          $http({method:'POST', url:'addpattern/'+$scope.value}).
                          success(function (data){
                              $scope.patterns = data;
                              $scope.state = false;
                          });
                      }
                  };
  
                  /*Function to update pattern
                  @param the pattern value
                  @return pattern lists
                  */
                  $scope.updatePattern = function(){
                      if($scope.newvalue == "" || $scope.selectedPattern == "")
                      {
                          alert("You must Enter all specified fields");
                      }
                      else
                      {
                          $http({method:'PUT', url:'updatepattern/'+$scope.selectedPattern+'/'+$scope.newvalue}).
                          success(function (data){
                              $scope.patterns = data;
                              $scope.state = false;
                          });                     
                      }
                      $scope.newvalue="";
                      $scope.selectedPattern="";
                  };
  
                  /*Function to update pattern
                  @param the pattern value
                  @return pattern lists
                  */
                  $scope.deletePattern = function(){
                      if($scope.id_pattern == "" )
                          {
                              alert("You must Enter all specified fields");
                          }
                      else 
                      {                       
                          $http({method:'DELETE', url:'deletepattern/'+$scope.id_pattern}).
                          success(function (data){
                              $scope.patterns = data;
                              $scope.state = false;
                          });
                      }   
                      $scope.id_pattern="";
                  };
                  $scope.value="";
                  $scope.state = true;
              });
          &lt;/script&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p><strong><em>web.xml</em></strong>
      
      
          contextConfigLocation
          /WEB-INF/spring/root-context.xml
      </p>
  
  <pre><code>&lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;!-- Processes application requests --&gt;
  &lt;servlet&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/spring/appServlet/servlet-context.xml&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;appServlet&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;/home1.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  </code></pre>
  
  <p>
  ***dispatcher****</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
          http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
           http://www.springframework.org/schema/data/jpa
      http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"&gt;
  
    &lt;!-- Enable annotation-based Spring MVC controllers (eg: @Controller annotation) --&gt;
    &lt;mvc:annotation-driven/&gt;
    &lt;context:annotation-config/&gt;
  
    &lt;!-- Classpath scanning of @Component, @Service, etc annotated class --&gt;
    &lt;context:component-scan base-package="com.demo.projet1.controllers" /&gt;
    &lt;context:component-scan base-package="com.demo.projet1.services" /&gt;
    &lt;context:component-scan base-package="com.demo.projet1.repositories" /&gt;
    &lt;jpa:repositories base-package="com.demo.projet1.repositories"/&gt;
  
    &lt;!-- Resolve view name into jsp file located on /WEB-INF --&gt;
    &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/views/" /&gt;
      &lt;property name="suffix" value=".jsp" /&gt;
    &lt;/bean&gt;
  </code></pre>
  
  <p></p>