0

I've seen lots of questions similar to mine but, I couldn't find a solution to this problem so far.

I am implementing a grid filtering and pagination on Spring + Hibernate. The load() method must receive the specific parameters for pagination (page, start and limit) and a list of key-value parameter for filtering, which is being the problem.

The parameters are coming like that:

page:1
start:0
limit:23
filter:[{"operator":"like","value":"tes","property":"desc"},{"operator":"like","value":"teste","property":"model_desc"}]

or (encoded version):

page=1&start=0&limit=23&filter=%5B%7B%22operator%22%3A%22like%22%2C%22value%22%3A%22tes%22%2C%22property%22%3A%22desc%22%7D%2C%7B%22operator%22%3A%22like%22%2C%22value%22%3A%22teste%22%2C%22property%22%3A%22model_desc%22%7D%5D

The filter parameter is coming as a String and the problem is to make Spring parse that either as something like ArrayList<Map<String,String>> or ArrayList<SomeFilterClass>.

This is the signature of my controller method (the commented lines are all not working, they are here just to show what I've tried so far):

public Map<String, Object> loadData(@RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "start", required = true) int start,
            @RequestParam(value = "limit", required = true) int limit,
//          @ModelAttribute("filter") ArrayList<Map<String, String>> filter) {
//          @RequestParam(value = "filter", required = false) Map<String, Object>[] filter) {
//          @RequestParam(value = "filter", required = false) List<Map<String, String>> filter) {
            @ModelAttribute("filter") RemoteFilter filter) 

This class, RemoteFilter, is a wrapper class that I built, following a suggestion from other posts but, it didn't work also. Its structure is:

public class RemoteFilter {

    private ArrayList<Filter> filter;

    //Getters and Setters....

    class Filter {
        private String operator;

        private String value;

        private String property;

        //Getters and Setters....
    }
}

I will be very glad if anybody help me with that.

Thanks!

1
  • 1
    It sounds like you are invoking the Controller via Ajax, is there any reason you using GET (and have to URL encode JSON) instead ofPOSTing and using Spring @RequestBody to convert the JSON into a Java object? Commented Jan 11, 2017 at 13:16

1 Answer 1

1

Try to POST the data instead of using GET, Spring only offers JSON to Java conversion when data is posted.

Post

{ 
    page:1
    start:0
    limit:23
    filter:[{"operator":"like","value":"tes","property":"desc"},{"operator":"like","value":"teste","property":"model_desc"}]
}

And have the controller use @RequestBody

@RequestMapping(method = RequestMethod.POST, value = "url", 
    produces = MimeTypeUtils.APPLICATION_JSON_VALUE, 
    consumes = MimeTypeUtils.APPLICATION_JSON_VALUE)
public Map<String, Object> loadData(@RequestBody RemoteFilter filter) {

}

The response uses Objectas the Map value type. This will work, but using an un-typped return value is a bad thing in general.

Sign up to request clarification or add additional context in comments.

8 Comments

Hi @KlausGroenbaek thanks for replying. I forgot to put the @RequestMapping annotation but it is a POST request already: @RequestMapping(value="/load", method=RequestMethod.POST). I changed it to @RequestBody(required=false) RemoteFilter filter and I got a error 415 (Unsupported Media Type). I thing this occurred because on first load, the parameter filter is not sent, that's why required is set to false.
415 means that the Content-type is incorrect. If you are posting from a browser it should add content-type=application/json automatically, in Spring you configure what a controllor method consumes and produces. I'll add that to the answer.
It's sending as Content-Type:application/x-www-form-urlencoded; charset=UTF-8. But one question, I'm still receiving the other parameters (page, start and limit). Does @RequestBody works in this situation still? @RequestParam(value = "page", required = false) int page, @RequestParam(value = "start", required = false) int start, @RequestParam(value = "limit", required = false) int limit, @RequestBody(required=false) RemoteFilter filter)
Content-Type:application/x-www-form-urlencoded is used for form submission. The filter parameter suggested that you were sending JSON. Does the page reload when you change page, or do you use JavaScript to load the new data, and then replace part of the page?
hmm got it.. First the page loads (without filter parameter), then user type any text on column filter and a new request is sent to server with filter data.. then the server returns new data and the page is refreshed..but, in all situations, the other parameters is sent (page, start and limit) that's why I think @RequestBody may not work, right?
|

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.