Spring Controller 404 returned after POST method Called

综合技术 2017-11-15

I have a Spring controller which is being called from JQuery.post(). When it is called the controller's method is invoked and returns. But then, in the background, Spring changes the URL and calls the server gain. The server responds with a 404.

I think this is in response to Spring trying to find a View after the POST method has been processed.

How do I stop the Spring controller from doing this.

Here is my Spring Controller:

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/person")
public class DataController {

  private List people = new ArrayList();

  @RequestMapping(value="put", method = RequestMethod.POST)
  public void addPerson(@ModelAttribute("person") Person person){
    System.out.println(">>>>>>> person: " + person);
    System.out.println(">>>>>>>>> " + person.getFirstName());
    people.add(person);
  }
}

Here is my application context xml file:



    

    

Here is my web.xml file:



    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath*:applicationContext-web.xml
        
        1
    

    
        dispatcherServlet
        /data/*
    

Here is my JQuery call from my HTML file:

function attachSendDataEvent(){
    $("#sendData").click(function(){

        var data = "firstName=" + $("#firstName").val() + "&" +
                "lastName=" + $("#lastName").val() + "&" +
                "address=" + $("#address").val() + "&" +
                "postcode=" + $("#postcode").val();

        $.post("data/person/put",
                data,
                dataSentOK
        );
    });

    return false;
}

The dataSentOK
function just does a alert("DONE")
.

So when the JQuery method the URL invoked is:

http://localhost:8080/jquery/data/person/put

and on the server side the System.out.println(...)
method print out the data as expected.

However in Firebug, the server sends back a 404.

So I switched on logging with Spring and got this:

[01] DispatcherServlet [DEBUG] DispatcherServlet with name 'dispatcherServlet' processing POST request for [/jquery/data/person/put]
[02] AbstractHandlerMethodMapping [DEBUG] Looking up handler method for path /person/put
[03] AbstractHandlerMethodMapping [DEBUG] Returning handler method [public void uk.co.jeeni.DataController.addPerson(uk.co.jeeni.Person)]
[04] AbstractBeanFactory [DEBUG] Returning cached instance of singleton bean 'dataController'
[05] DispatcherServlet [DEBUG] Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'person/put'; URL [person/put]] in DispatcherServlet with name 'dispatcherServlet'
[06] AbstractView [DEBUG] Added model object 'org.springframework.validation.BindingResult.person' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'person/put'
[07] AbstractView [DEBUG] Added model object 'person' of type [uk.co.jeeni.Person] to request in view with name 'person/put'
[08] InternalResourceView [DEBUG] Forwarding to resource [person/put] in InternalResourceView 'person/put'
[09] DispatcherServlet [DEBUG] DispatcherServlet with name 'dispatcherServlet' processing POST request for [/jquery/data/person/person/put]
[10] AbstractHandlerMethodMapping [DEBUG] Looking up handler method for path /person/person/put
[11] AbstractHandlerMethodMapping [DEBUG] Did not find handler method for [/person/person/put]
[12] DispatcherServlet [ WARN] No mapping found for HTTP request with URI [/jquery/data/person/person/put] in DispatcherServlet with name 'dispatcherServlet'
[13] FrameworkServlet [DEBUG] Successfully completed request
[14] FrameworkServlet [DEBUG] Successfully completed request

In response to the URL POST request (/jquery/data/person/put), the correct method is found and invoked (lines 1 to 7), but then Spring forwards to an InternalResourceView
at line 8, which changes the URL to /jquery/data/person/person/put
, and this can not be found.

How do I stop Spring from trying to find a view at all. All I want it to do is return cleanly and be done.

Thanks for your help.

Solved.

The problem was as #CodeChimp suggested, except I still wanted a void return type.

I added the @ResponseBody
to the addPerson
method and everything worked fine:

@RequestMapping(value="put", method = RequestMethod.POST)
**@ResponseBody**
public void addPerson(@ModelAttribute("person") Person person){
  System.out.println(">>>>>>> person: " + person);
  System.out.println(">>>>>>>>> " + person.getFirstName());
  people.add(person);
}

The clue came from http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-responsebody
. Although the documentation is not clear what happens with a void return. Just tried it and it worked.

您可能感兴趣的

Command to load jQuery on the Chrome Inspector&... I remember seeing that there was a specific command you could put on Google Chrome's inspector console for it to load jQuery and allow you to execute ...
I want to hide EditText jQuery - want to hide a rich line: dataTable I have a rich:dataTable. I want to hide a row with this code:
JQuery Nested Conditional Is it possible to nest conditional statements in JQuery? The code below works if I strip off the outside if but once I add it back it stops working...
Jquery ajax json data truncate zero values I'm retrieving a json string as below with jquery ajax and displaying it on a xhtml page {"pID":"T1","avBal":147765035.20,"accBalance":147713417.00}...
常用开源框架中设计模式使用分析 一、前言 说起来设计模式,大家应该都耳熟能详,设计模式代表了软件设计的最佳实践,是经过不断总结提炼出来的代码设计经验的分类总结,这些模式或者可以简化代码,或者可以是代码逻辑开起来清晰,或者对功能扩展很方便…。 设计模式按照使用场景可以分为三大类:创建型模式(Creational Patter...