Spring Controller 404 retuned after POST method Invoked

后端 未结 2 424
野趣味
野趣味 2021-01-11 16:19

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 c

相关标签:
2条回答
  • 2021-01-11 16:42

    I believe if you have a null or void return type, Spring will try to resolve the view based on the request URL. I think the proper form here would be to simply return an OK page, since this doesn't appear to be JSON or anything like that. Or, just tag it with @ResponseBody and return an empty string.

    0 讨论(0)
  • 2021-01-11 16:59

    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.

    0 讨论(0)
提交回复
热议问题