1

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.

Tomcat start up shows this line in console :

INFO: Mapped "{[/findDepartment],methods=[GET]}" onto public org.springframework.http.ResponseEntity com.erp.controller.AdminController.findDepartment(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
Naveen
  • 773
  • 3
  • 17
  • 40

1 Answers1

1

Change your servlet mapping to "/", so that dispatcher servlet can read all the requests. Here you are sending request to the server which dispatcher servlet can't handle. So basically change dispatcher servlet mapping to this-

 <servlet-mapping>
 <servlet-name>erp</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>
Siddharth Gharge
  • 112
  • 3
  • 13
  • main page is stopped loading after this change, this is in eclipse console : `org.springframework.web.servlet.PageNotFound noHandlerFound WARNING: No mapping found for HTTP request with URI [/TexERP/] in DispatcherServlet with name 'erp'` – Naveen Oct 22 '17 at 07:52
  • 1
    `` was missing in -servlet.xml file and i changed `url-pattern` to '/'. Found here `https://stackoverflow.com/questions/24837383/org-springframework-web-servlet-pagenotfound-nohandlerfound-warning-no-mapping` – Naveen Oct 22 '17 at 07:58