0

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 http://localhost:8088/angular/demo to get some json data from the server. But I get a 406 not acceptable error for this.

enter image description here

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.

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.

enter image description here

In index.jsp i have:

$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();

The controller is :

@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<Data> getAllTasks() {  
   List<Data> tasks=new ArrayList<Data>() ;
   Data data=new Data();
   data.setName("Abc");
   data.setEmail("abc@xyz.com");
   tasks.add(data);
   return tasks;     
  }   
}

The resource representation class is:

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;
    }
}

web.xml:

<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">

   <display-name>Angular Rest Spring</display-name>

   <servlet>
      <servlet-name>angular</servlet-name>
      <servlet-class>
         org.springframework.web.servlet.DispatcherServlet
      </servlet-class>
   </servlet>

   <servlet-mapping>
      <servlet-name>angular</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

</web-app>

angular-servlet.jsp :

<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">

   <context:component-scan base-package="com.gitanjal.angular"/>
   <mvc:annotation-driven />

   <bean id="viewResolver"      class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/" />
      <property name="suffix" value=".jsp" />
   </bean>
</beans>
B.Mayssa
  • 94
  • 13
sargenem
  • 45
  • 1
  • 7
  • 2
    Check if you have the libs `jackson-core-asl-1.9.X.jar jackson-mapper-asl-1.9.X.jar` in your classpath. See http://stackoverflow.com/questions/7462202/spring-json-request-getting-406-not-acceptable – Michael Sep 25 '15 at 15:46
  • thank you very much Michael , i missed those files . – sargenem Sep 25 '15 at 16:03

0 Answers0