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.

mrkb80
  • 581
  • 2
  • 8
  • 25
  • 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. – Sotirios Delimanolis Feb 17 '14 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}]} – mrkb80 Feb 17 '14 at 16:29
  • 2
    Change your AJAX request to send the content-type as `application/json`. – Sotirios Delimanolis Feb 17 '14 at 16:30
  • tried this, but still get unsupported media type: `$.post("/ListRequest", {myList: myList}, 'application.JSON')` also tried `$.post("/ListRequest", {myList: myList}, 'JSON')` – mrkb80 Feb 17 '14 at 16:32
  • Sorry, that should be `application/json`. – Sotirios Delimanolis Feb 17 '14 at 16:33
  • nope `application/json' still is unsupported media type. – mrkb80 Feb 17 '14 at 16:34
  • I'm not very familiar with jQuery. Try a full [`$.ajax`](https://api.jquery.com/jQuery.post/) call. I think you're missing an element from your `$.post`. Also, enable debug logging in your web app. – Sotirios Delimanolis Feb 17 '14 at 16:37
  • json is normally passed as RequestBody rather than params - have you tried the @RequestBody annotation instead. Similar question here: http://stackoverflow.com/a/3921780/258813 Does this answer work for you? – rhinds Feb 17 '14 at 16:40
  • changed to RequestBody and switched the call to ajax...but still no luck. `The request sent by the client was syntactically incorrect.` – mrkb80 Feb 17 '14 at 16:44
  • Let's see your ListT class. – Sotirios Delimanolis Feb 17 '14 at 16:51
  • 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. – Sotirios Delimanolis Feb 17 '14 at 18:31

3 Answers3

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');
            }
        });
jpprade
  • 3,497
  • 3
  • 45
  • 58
  • turns out I was getting a 404 error because the controller was not returning anything, but the request was actually totally correct. – mrkb80 Feb 20 '14 at 03:13
  • 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 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?! – Web User Nov 14 '14 at 18:13
  • 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 – jpprade Nov 15 '14 at 16:55
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

Community
  • 1
  • 1
Morez
  • 2,048
  • 5
  • 36
  • 49
-1

You can try

@RequestParam("myList") myList tempmyList


@Param myList tempmyList

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

code-jaff
  • 9,230
  • 4
  • 35
  • 56