Sunday, October 22, 2017

Angularjs $http spring rest controller – 404 error

I am getting 404 error on making a $http call from angularjs to spring controller, here is the code :

Factory :

factory.checkCodeAvail = function(url){
        return $http({
            url: url,
            responseType:"json",
            method: "GET",
            headers: {
                "Content-Type": "application/json"
            }
        });
}

This factory method is called by controller :

commonFactory.checkCodeAvail('findDepartment')
                    .then(function (success){
                        console.log(success);
                   },function (error){
                       console.log(error);           
});

This is the error i m getting in browser console :

GET http://localhost:8080/TexERP/findDepartment 404 ()

Spring controller :

@RestController
public class AdminController {

    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);

    @RequestMapping(value="/findDepartment", method=RequestMethod.GET)
    public ResponseEntity findDepartment(HttpServletRequest req, HttpServletResponse res){
        ResponseEntity response = null;


        return response;
    }

}

servlet file.xml:

 <context:annotation-config />
 <!-- <cache:annotation-driven /> -->

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

web.xml

<?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" 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>TexERP</display-name>

  <servlet>
    <servlet-name>erp</servlet-name>
    <servlet-class>
          org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <listener>
       <listener-class>
          org.springframework.web.context.ContextLoaderListener
       </listener-class>
    </listener>

  <context-param>
       <param-name>contextConfigLocation</param-name>
       <param-value>/WEB-INF/erp-servlet.xml</param-value>
    </context-param>



<servlet-mapping>
    <servlet-name>erp</servlet-name>
    <url-pattern>/TexERP/*</url-pattern>
  </servlet-mapping>
</web-app>

Ajax call is not reaching spring controller.

Source: AngularJS



from Angular Questions https://angularquestions.com/2017/10/22/angularjs-http-spring-rest-controller-404-error/
via @lzomedia #developer #freelance #web #lzomedia.com

No comments:

Post a Comment