简体   繁体   中英

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. 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):

@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:

$("#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. Why do i get that error? and how can i solve it? Thanks!

Do you have a @RequestMapping(value="/jsonspringhibernateexample") before your controler class ? Else try to remove headers = {"Content-type=application/json"} , maybe the content-type is wrong (but I'm not sure).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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