Search code examples
javarestspring-mvcconfigurationspring-config

Spring MVC Will Not Map to My Controller


I am trying to migrate a project from a traditional (working) Java servlet application to Spring MVC in the NetBeans IDE, but my program absolutely refuses to ping the Controller. On the client side, I see the following 404 errors:

index.do?displayType=table:1 Failed to load resource: the server responded with a status of 404 (Not Found)

index.do?displayType=table:1 Failed to load resource: the server responded with a status of 404 (Not Found)

I invoke the controller methods like so:

$.get("index.do","displayType=table", function( data ) {
    jstring = JSON.parse(data.substr(12).slice(0, -1));  

});

$.get("index.do","displayType=pivot",function(unparsedJSON) {});

$.post("index.do","jsonString=" + JSON.stringify(hot.getData()));

Below are my controller and xml configuration files:

MappingControl.java

package controllers;

import infoLoader.JsonWriter;
import infoLoader.getJSON;
import java.util.logging.Level;
import java.util.logging.Logger;
import static org.springframework.http.HttpMethod.GET;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;



@Controller
@RequestMapping("/")
public class MappingControl {

    @RequestMapping(value="/index.do", method=RequestMethod.GET)
    public String populatePivotAndSheet(@RequestParam("displayType") String type) {

        String returnedJSON = "";

        try {
             returnedJSON = getJSON.getJSON(type);
        } catch (Exception ex) {
            System.out.println("Unable to retrieve JSON");
        }

        return returnedJSON;

    }

    @RequestMapping(value="/index.do", method=RequestMethod.POST)
    public void deliverSheet(@RequestParam("jsonString") String writableJSON) {

        String returnedJSON = "";

        JsonWriter.writeJSON(writableJSON);

    }

}

applicationContext.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

</beans>

dispatcher-servlet.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd" xmlns:mvc="http://www.springframework.org/schema/mvc">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <!--
    Most controllers will use the ControllerClassNameHandlerMapping above, but
    for the index controller we are using ParameterizableViewController, so we must
    define an explicit mapping for it.
    -->
    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="index.htm">indexController</prop>
            </props>
        </property>
    </bean>

    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"
          p:prefix="/WEB-INF/jsp/"
          p:suffix=".jsp" />

    <!--
    The index controller.
    -->
    <bean name="indexController"
          class="org.springframework.web.servlet.mvc.ParameterizableViewController"
          p:viewName="index" />

    <mvc:resources mapping="/resources/**" location="/resources" />
    <context:component-scan base-package="controllers" />


</beans>

I've been building this based on the default Spring-MVC template provided by NetBeans, so if any of you believe the template is poorly formatted and should be changed in some way, I would appreciate any input you might have.

Thanks so much for your time, guys, and let me know if anything is unclear.


Solution

  • You need add @ResponseBody to your method,due to in your case,you want to return the json data,if you missing @ResponseBody it will return to the view page,however you do not specify any view page in your code,thus will cause 404 error

    @ResponseBody
    @RequestMapping(value="/index.do", method=RequestMethod.GET)
    public String populatePivotAndSheet(@RequestParam("displayType") String type) {
    
        String returnedJSON = "";
    
        try {
             returnedJSON = getJSON.getJSON(type);
        } catch (Exception ex) {
            System.out.println("Unable to retrieve JSON");
        }
    
        return returnedJSON;
    
    }
    
    @ResponseBody
    @RequestMapping(value="/index.do", method=RequestMethod.POST)
    public void deliverSheet(@RequestParam("jsonString") String writableJSON) {
    
        String returnedJSON = "";
    
        JsonWriter.writeJSON(writableJSON);
    
    }