2

I'm trying to set up my Tomcat server to handle AJAX requests with a Spring Controller, however I am getting 404 errors when the AJAX request is sent. I'm using IntelliJ with Maven, and ultimately trying to set up a website to handle a login service and other data services.

Here's what I have:

pom.xml - path: myWebapp/pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mygroup.app</groupId>
    <artifactId>myWebapp-webapp</artifactId>
    <packaging>jar</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>myWebapp-webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>3.2.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>

web.xml - path: myWebapp/src/web/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           version="2.5">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/data/*</url-pattern>
    </servlet-mapping>
</web-app>

dispather-servlet.xml - path: myWebapp/src/web/WEB-INF/dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.0.xsd">

    <context:component-scan base-package="com.myWebapp.sdc.controllers"  />
    <mvc:annotation-driven />
</beans>

CredentialsController.java - path: myWebapp/src/main/java/com/myWebapp/sdc/controllers/CredentialsController.java. The ServiceRequest<> class works fine and is a standard template.

package com.myWebapp.sdc.controllers;

import com.myWebapp.classes.LoginDataModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.myWebapp.classes.ServiceRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "login")
public class CredentialsController {
    public @ResponseBody ServiceRequest<LoginDataModel> login(
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ServiceRequest<LoginDataModel> loginDataModelServiceRequest = new
                ServiceRequest<LoginDataModel>(new 
        return loginDataModelServiceRequest;
    }
}

and lastly, the AJAX call - inside html (path: myWebapp/src/web/index.html). The default launch page is http://localhost:8080/web/index.html. And I tested that JQuery is loaded correctly before the call is made.

$(document).ready(function(){
    $("#submitLogin").on('click', function(){
        $.ajax({
            type:"POST",
            url: '/data/login',
            success:function(data){
                alert('success');
            },
            error:function(){
                alert('failed');
            }
        });
    });
});

and the XHR call in Chrome Dev Tools:

Request URL:http://localhost:8080/data/login
Request Method:POST
Status Code:404 Not Found
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0
Cookie:JSESSIONID=6717B5682C531D61F05270196DC09DFD
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/web/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31
X-Requested-With:XMLHttpRequest
Response Headersview source
Content-Length:971
Content-Type:text/html;charset=utf-8
Date:Sun, 12 May 2013 00:58:04 GMT
Server:Apache-Coyote/1.1

Any input would be appreciated! Thanks so much :)

UPDATE AND CLOSING: UNRESOLVED I wasn't able to figure this out unfortunately. Please consult for help elsewhere on this problem. I ended up using Node as a server instead.

rsteier
  • 154
  • 2
  • 12
  • You got to check that the address was actually mapped correctly on application startup? You start in the spring mvc can see all RequestMapping. – Tiarê Balbi May 12 '13 at 05:43
  • Thanks for the response! I tried that based on [this SO answer](http://stackoverflow.com/a/10898139/712849). And yes, it prints to console `com.myWebapp.sdc.controllers.CredentialsController`. So that must mean the call is bad then. – rsteier May 12 '13 at 14:09

1 Answers1

0

You didn't annotate your login method in the controller with @RequestMapping. So if your code is

@RequestMapping
public @ResponseBody ServiceRequest<LoginDataModel> login(
....

then the url /data/login will be mapped.

lunr
  • 5,159
  • 4
  • 31
  • 47
  • Could you clarify? Would adding a `@RequestMapping(value = "log")` before the `login` method suffice for you? I have tried that (and updated the AJAX call to `/data/login/log`) but to no avail – rsteier May 12 '13 at 02:32
  • Oh i see you're using `POST` method. Can you try again with `@RequestMapping(value = "log", method = RequestMethod.POST)`? – lunr May 12 '13 at 02:44
  • Yes, just tried that and that did not work either. Thanks for the reply. Any other thoughts? Is there a way to check whether the controller is being picked up by Spring, perhaps? – rsteier May 12 '13 at 02:53
  • you're posting to `/data/login/log`, that should be `myWebapp-webapp/data/login/log`, where `myWebapp-webapp` is your application name. – lunr May 12 '13 at 03:19
  • I think that is `web/data/login/log` in your case. – lunr May 12 '13 at 03:32
  • That did not work either. I'm going to try and get some logging going and see what's going on underneath. In the meantime, my IDE (IntelliJ) is picking up my Controller call and associating it with `` in the dispatcher-servlet.xml file. So there must be something going wrong in the request then, correct? – rsteier May 12 '13 at 04:01
  • I see the update to your original post. I tried the `/data/login`, `myWebapp-webapp/data/login/log`, and `web/data/login/log`. Nothing has worked. Any other suggestions? – rsteier May 12 '13 at 13:34
  • I am out of ideas :) I created a sample project and it is working. Are you sure spring is starting up correctly? There may be some misspeling somewhere. You may try to configure Log4j to make Spring log verbose messages during startup. – lunr May 12 '13 at 19:17
  • Thanks for your help with this @lunr! I didn't end up resolving this, unfortunately. I set up a Node server instead, which took less than half the time to do than trying to debug this whole thing. – rsteier May 16 '13 at 14:56