SJS
SJS

Reputation: 5667

JSON Posting to Spring-MVC, Spring is not seeing the data

I am working on a project that the project is going to use Ajax to post JSON object to Springs-MVC. I been making a number of changes and I got it to the point where I dont get any more errors BUT I dont see the data that is getting POSTed to Spring in the object I need it in.

Here is my Spring Controller.

@RequestMapping(value="/AddUser.htm",method=RequestMethod.POST)
    public @ResponseBody JsonResponse addUser(@ModelAttribute(value="user") User user, BindingResult result ){
        JsonResponse res = new JsonResponse();

        if(!result.hasErrors()){
            res.setStatus("SUCCESS");
            res.setResult(userList);
        }else{
            res.setStatus("FAIL");
            res.setResult(result.getAllErrors());
        }

        return res;
    }

I put a breakpoint in and my USER object never gets the data. next is a copy of my USER object:

public class User {

    private String name = null;
    private String education = null;

    private List<String> nameList = null;
    private List<String> educationList = null;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEducation() {
        return education;
    }
    public void setEducation(String education) {
        this.education = education;
    }
    public List<String> getNameList() {
        return nameList;
    }
    public void setNameList(List<String> nameList) {
        this.nameList = nameList;
    }
    public List<String> getEducationList() {
        return educationList;
    }
    public void setEducationList(List<String> educationList) {
        this.educationList = educationList;
    }

and now for the javascript code that does the Ajax, JSON post:

function doAjaxPost() {  

      var inData = {};

      inData.nameList = ['kurt','johnathan'];
      inData.educationList = ['GSM','HardKnocks'];

      htmlStr = JSON.stringify(inData);
      alert(".ajax:" + htmlStr);


    $.ajax({
         type: "POST",
         contentType: "application/json; charset=utf-8",
         url:  contexPath + "/AddUser.htm",
         data: inData,
         dataType: "json",
         error: function(data){
              alert("fail");
         },
         success: function(data){
              alert("success");
         }
         });

};

Please let me now if you can help?? I have to get this working ASAP... thanks

Upvotes: 0

Views: 8308

Answers (1)

Daniel Lavoie
Daniel Lavoie

Reputation: 1922

You also need to specify the header in your RequestMapping annotion found in your controller.

@RequestMapping(headers ={"Accept=application/json"}, value="/AddUser.htm", method=RequestMethod.POST)

Also, remove .htm in your URL path. htm is some kind of request type overide. Using .htm specifies the web server to handle the request as a classic html request. Using .json would specify to the webserver that the request expects to be handled as a json request.

Upvotes: 3

Related Questions