0

I am trying to pass a JSON array into spring MVC Controller like such:

 var myList = new Array(); 
data._children.forEach( function (d) { 
                        myList.push( {NAME: d.name, TYPE: d.TYPE, FDATE: d.FDATE } );
                    });

 $.post("/ListRequest", {myList: myList});

The controller looks as such:

 @RequestMapping(value="/ListRequest", method = RequestMethod.POST)
    public void ListRequest(@RequestParam("myList") myList tempmyList )
    {
        System.out.println(tempmyList);
    }

The class myList defined as such:

public class MyList {
    private List<ListT> ListT;
    public List<ListT> getListT() {
        return ListT;
    }

    public void setListT(List<ListT> listT) {
        ListT = listT;
    }

}

ListT class:

public class ListT {
    private String NAME;
    private String TYPE;
    private Long FDATE; ...

I keep getting this error: HTTP Status 400 - Required myList parameter 'myList' is not present

Also tried this request:

$.ajax({
                type: "post",
                url: "ListRequest", //your valid url
                contentType: "application/json", //this is required for spring 3 - ajax to work (at least for me)
                data: JSON.stringify(myList), //json object or array of json objects
                success: function(result) {
                    //do nothing
                },
                error: function(e){
                    alert('failure');
                }

but get this error: JBWEB000120: The request sent by the client was syntactically incorrect.

11
  • 1
    Open your browser's network console and check what JSON is actually written in the request. Post that here. Also, you will need to use @RequestBody rather than @RequestParam. I'll let you look up why. Commented Feb 17, 2014 at 16:26
  • I tried the @RequestBody, but I get an unsupported mediatype error. Also the JSON looks like this: {"myList":[{"NAME":"Blah","TYPE":"TYPE1","DATE":1385874000000}]} Commented Feb 17, 2014 at 16:29
  • 2
    Change your AJAX request to send the content-type as application/json. Commented Feb 17, 2014 at 16:30
  • tried this, but still get unsupported media type: $.post("/ListRequest", {myList: myList}, 'application.JSON') also tried $.post("/ListRequest", {myList: myList}, 'JSON') Commented Feb 17, 2014 at 16:32
  • 1
    You have to write your class so that it matches the JSON or vice versa. Spring uses jackson behind the scenes so figure out how that mapping should be done and fix it if required. Commented Feb 17, 2014 at 18:31

3 Answers 3

2

try to add this to you ajax call it should fix the unsupported response :

headers : {
    'Accept' : 'application/json',
    'Content-Type' : 'application/json'
},

this is a full example of ajax call that is working for me :

$.ajax({
            dataType : "json",
            url : this.baseurl + "/dataList",
            headers : {
                'Accept' : 'application/json',
                'Content-Type' : 'application/json'
            },
            data : JSON.stringify(params),
            type : 'POST',
            success : function(data) {
                self.displayResults(data);
            },
            error : function(jqXHR,textStatus,errorThrown ){
                showPopupError('Error','error : ' + textStatus, 'ok');
            }
        });
Sign up to request clarification or add additional context in comments.

3 Comments

turns out I was getting a 404 error because the controller was not returning anything, but the request was actually totally correct.
I want to use the technique quoted in this thread. My Ajax call specifies contentType: 'application/json', dataType: 'json' and data: {"values":JSON.stringify(['String1','String2','String3'])}. In my controller, I annotate controller method argument as @RequestParam List<String> valuesList. The controller method is invoked, but when I examine the values in valuesList, I see: ["String1", "String2" and "String3"]! What am I doing wrong?!
Seem like you are using GET, I not sur deserialization occurs with @RequestParam, try to send you data in post and use @RequestBody in your controller
0

Even I was facing same problem. My client request was right and generated proper json file. but still i was getting same error.

I solved this error using @JsonIgnoreProperties(ignoreUnknown = true) in pojo class.

refer this link. Spring MVC : The request sent by the client was syntactically incorrect

Comments

-1

You can try

@RequestParam("myList") myList tempmyList


@Param myList tempmyList

in addition, class names must begin with a capital letter.

2 Comments

What have you changed? What is @Param? Class name must not start with uppercase letter, they should start with uppercase letter.
fixed the uppercase, sorry about that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.