简体   繁体   English

找不到HTTP / 1.1 404-JSON Spring MVC

[英]HTTP/1.1 404 Not Found - JSON Spring MVC

I write 1 webapp,i try to send 1 JSON to server and save it into MySQL. 我写了1个webapp,我尝试将1个JSON发送到服务器并将其保存到MySQL中。 I've got this problem: 我有这个问题:

POST http://localhost:8080/jsonspringhibernateexample/addclass [HTTP/1.1 404 Not Found 910ms]

This is my controller (use to add one Class to Mysql): 这是我的控制器(用于向Mysql添加一个类):

@Controller
public class ClassController {

    @Autowired
    private ClassService classService;
    @RequestMapping(value ="/index",method = RequestMethod.GET)
    public String getIndex(ModelMap map){
        map.put("listClass",classService.getListClass());
        map.put("studentClass", new Class());
        return "class";
    }
    @RequestMapping(value ="/addclass", method = RequestMethod.POST)
    public String addClass(@RequestBody Class studentClass){
        classService.addClass(studentClass);
        return "index";
    }
}

This is my jquery to call controller: 这是我的jQuery来调用控制器:

$("#addclass").click(function(e){
    $.ajax({
        url:"addclass",
        type: "post",
        contentType: 'application/json',
        data: JSON.stringify({className:$("#input").val(),listStudent:null}),
    }).done(function(){
        $("#listclass").appent("<option>"+$("#input").val()+"</option>");
    });
});

When i click "#addclass" button, the class was added but controller not return index.jsp, and i got my trouble. 当我单击“ #addclass”按钮时,该类已添加,但控制器未返回index.jsp,这给我带来了麻烦。 Why do i get that error? 为什么会出现该错误? and how can i solve it? 而我该如何解决呢? Thanks! 谢谢!

Do you have a @RequestMapping(value="/jsonspringhibernateexample") before your controler class ? @RequestMapping(value="/jsonspringhibernateexample")类之前是否有@RequestMapping(value="/jsonspringhibernateexample") Else try to remove headers = {"Content-type=application/json"} , maybe the content-type is wrong (but I'm not sure). 否则尝试删除headers = {"Content-type=application/json"} ,也许content-type是错误的(但我不确定)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM