title: How to send json from spring-mvc controller to angularjs
body: <p>I am trying to develop a simple application using angularjs and spring mvc restful web service. At first, I have to load the index file where it makes an http call <a href="http://localhost:8088/angular/demo" rel="nofollow noreferrer">http://localhost:8088/angular/demo</a> to get some json data from the server. But I get a 406 not acceptable error for this.</p>
  
  <p><a href="https://i.stack.imgur.com/gSpGb.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gSpGb.jpg" alt="enter image description here"></a>  </p>
  
  <p>The first method in the controller, I have written to load the main page and the second method should respond with user information as JSON. </p>
  
  <p>Is it wrong to have a method returning modelAndView and another returning data in the same controller?
  If I use only methods serving JSON data in the controller, then how should I load the first page? 
  I have included all the required libraries in the lib folder.</p>
  
  <p><a href="https://i.stack.imgur.com/GVv6O.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GVv6O.jpg" alt="enter image description here"></a></p>
  
  <p>In index.jsp i have:</p>
  
  <pre><code>$scope.displayUsers=function(){
  
  
    $http({
    method  : 'GET',
    url     : urlBase+'/demo'
   }).success(function(response) {
       var response = JSON.parse(response)
            alert(response);
          console.log(response);
  
  
  
        });
  }
  
  //---call the display users method
  $scope.displayUsers();
  </code></pre>
  
  <p>The controller is :</p>
  
  <pre><code>@RestController
  public class Controller {
  
  @RequestMapping("/")
  public ModelAndView helloWorld()
  {
      ModelAndView model=new ModelAndView("index");
      model.addObject("msg", "Hello World");
  
      return model;
  }
  
  
  @RequestMapping(value="/demo", method = RequestMethod.GET)  
    public @ResponseBody List&lt;Data&gt; getAllTasks() {  
     List&lt;Data&gt; tasks=new ArrayList&lt;Data&gt;() ;
     Data data=new Data();
     data.setName("Abc");
     data.setEmail("abc@xyz.com");
     tasks.add(data);
     return tasks;     
    }   
  }
  </code></pre>
  
  <p>The resource representation class is:</p>
  
  <pre><code>public class Data {
      String name;
      String email;
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public String getEmail() {
          return email;
      }
      public void setEmail(String email) {
          this.email = email;
      }
  }
  </code></pre>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;web-app id="WebApp_ID" version="2.4"
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  
     &lt;display-name&gt;Angular Rest Spring&lt;/display-name&gt;
  
     &lt;servlet&gt;
        &lt;servlet-name&gt;angular&lt;/servlet-name&gt;
        &lt;servlet-class&gt;
           org.springframework.web.servlet.DispatcherServlet
        &lt;/servlet-class&gt;
     &lt;/servlet&gt;
  
     &lt;servlet-mapping&gt;
        &lt;servlet-name&gt;angular&lt;/servlet-name&gt;
        &lt;url-pattern&gt;/&lt;/url-pattern&gt;
     &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>angular-servlet.jsp :</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="
     http://www.springframework.org/schema/beans     
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/mvc     http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"&gt;
  
     &lt;context:component-scan base-package="com.gitanjal.angular"/&gt;
     &lt;mvc:annotation-driven /&gt;
  
     &lt;bean id="viewResolver"      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
        &lt;property name="prefix" value="/WEB-INF/" /&gt;
        &lt;property name="suffix" value=".jsp" /&gt;
     &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>