-1

I have the already working javascript AJAX POST like this:

Client side:

<script language="JavaScript" type="text/javascript">
    var column = "test1";
    var filterType = 1;
    var values = [];
    var filter = { "column" : column, "filterType" : filterType, "values": values};
    var filter2 = { "column" : column, "filterType" : filterType, "values": values};
    filter2.column = "test2";
    var filters = new Array();
    filters[0] = filter;
    filters[1] = filter2;

    $.ajax({  
        url: "${pageContext.request.contextPath}/api/user_administration/get", 
        data: JSON.stringify(filters),  
        type: "POST", 
        beforeSend: function(xhr) 
        {  
            xhr.setRequestHeader("Accept", "application/json");  
            xhr.setRequestHeader("Content-Type", "application/json");  
        },
        success: function(user)
        {  

        }
    });
</script>

Server side:

@RequestMapping(value = "user_administration/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<User> get(@RequestBody ColumnFilter[] filters)
{
    //do something
    return userService.getAll();
}

Now I want to pass two or more parameters. Something like this:

@RequestMapping(value = "user_administration/get", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<User> get(@RequestBody ColumnFilter[] filters, @RequestBody ColumnSorter[] sorters)
{
    //do something
    return userService.getAll();
}

Is it possible to achieve this? How?

(I already am aware that I can just encapsulate them into another object/class/entity)

William Wino
  • 3,599
  • 7
  • 38
  • 61
  • I think you could encapsulate it as you said or add the other params to request header and then you can access them by param name. – jakub.petr Nov 13 '13 at 09:52
  • @jakub.petr tell me more about the "other params to request header" – William Wino Nov 13 '13 at 09:56
  • JS: http://stackoverflow.com/questions/3258645/pass-request-headers-in-a-jquery-ajax-get-call Spring: http://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch16s11.html Personally, I think better is to encapsulate it to other entity and pass in requestBody – jakub.petr Nov 13 '13 at 10:00
  • Don't forget that the headers have a size limit (it depends on the server). – V G Nov 13 '13 at 10:03

2 Answers2

0

In your AJAX call you have the line

 data: JSON.stringify(filters),  

Change or extend this line to add additional parameters to the request. And if you're not clear what's going on or need to log/diagnose/debug it, you can always use an intermediate variable..

You can either pass data as a Javascript key-value object (giving named parameters), a String, or -- as the existing code does -- by JSON encoding an object.

Since you appear to want to pass more fields/parameters & are using JSON encoding already, it probably makes most sense to built these into a larger 'payload' object before JSON encoding.

 var paramData = {filter:filters, sorters: mySortersOrWhatever};
 console.log('some administration AJAX request', paramData);
 $.ajax({ 
     ...
     data: JSON.stringify(paramData)

See how this interfaces with Spring's request-parsing -- to decode two separate parameters on the Spring side you'll either need names (not just the request-body), decode them from named parameters, or else (simplest) just accept one big JSON object as the parameter.

This should be reasonably straight-forward -- as always, follow basic software-engineering principles (log it, diagnose it, fix it until it works) so you can build the required working functionality.

Thomas W
  • 13,940
  • 4
  • 58
  • 76
  • I've already mentioned that I was already aware that I could just encapsulate them. Read the bottom of the description. – William Wino Nov 13 '13 at 10:27
  • Well, all the JSON examples I can find require `@RequestBody` and are quite clear about that. But you're welcome to try `@RequestParam("filters")` and `@RequestParam("sorters")`.. see this post which talks about custom code to make it possibly work. http://stackoverflow.com/questions/8946098/spring-mvc-deserialize-single-json Let us know how you go.. and I'm sorry you come across as unappreciative. – Thomas W Nov 14 '13 at 08:31
  • Thanks for the link. I merely stated the fact that I did mention that in my description. I didn't insult you or say that you're wrong and I am not obligated to favor every answer that is thrown at me. No need to take it personally. I don't know who downvoted my question, but if it was you, it isn't wise to downvote a valid question for something that I say to you because it has nothing to do with the question itself. Ad Hominem. – William Wino Nov 14 '13 at 09:38
  • 1
    Fair enough, thanks for your comments & let us know how you go with `@RequestParam`. – Thomas W Nov 14 '13 at 18:54
  • 1
    @RequestParam didn't work, it returns a 400 bad request error. it says that the syntax isn't correct or something. I haven't tried the custom `WebArgumentResolver` from the link you gave me. I'll just go with the encapsulated parameters. Thanks. – William Wino Nov 15 '13 at 06:29
0

Spring documentation says:

The @RequestBody method parameter annotation indicates that a method parameter should be bound to the value of the HTTP request body.

This assumes that HTTP request body is not divisible. Hence all you need to do is to follow the workaround.Here you can see exactly the same question Spring REST multiple @RequestBody parameters, possible?

Community
  • 1
  • 1
mvb13
  • 1,514
  • 3
  • 18
  • 33
  • I've already mentioned that I was already aware that I could just encapsulate them. Read the bottom of the description. – William Wino Nov 13 '13 at 10:27
  • I red this one. But the answer on your question is on 90% no. You can't do what you want. – mvb13 Nov 13 '13 at 10:30