Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request param list of long

eventIdsToDelete ["24616342","24615878"]I am trying to send list of Long values to the Spring controller but I am getting 400 Bad Request error. So I am guessing my request mapping signature is incorrect.

My jQuery AJAX call

var myList = [24616342,24616201,24616310];
$.ajax({
  url: '/myApp/path/toController',
  type: 'POST',
  data: {myList: JSON.stringify(myList)},
  success: function(response) { ... }
}); 

My request mapping

@RequestMapping(value = "/myApp/path/toController", method = RequestMethod.POST)
public @ResponseBody boolean doSomething(Model model, @RequestParam List<Long> myList)   
{
    System.out.println(myList);
    return true;
}

In firebug console I am seeing URL being called but in post tab I am seeing parameters as

myList ["24616342","24615878"]

I tried by changing request mapping parameter to

List<String> 

and it working fine. But I want to have request mapping method type be String.

like image 800
αƞjiβ Avatar asked Nov 14 '14 17:11

αƞjiβ


People also ask

What is the use of @param annotation?

In Spring MVC, the @RequestParam annotation is used to read the form data and bind it automatically to the parameter present in the provided method. So, it ignores the requirement of HttpServletRequest object to read the provided data.

What is the difference between @PathVariable and @RequestParam annotation?

2) @RequestParam is more useful on a traditional web application where data is mostly passed in the query parameters while @PathVariable is more suitable for RESTful web services where URL contains values.

What is difference between @RequestParam and @QueryParam?

@QueryParam is a JAX-RS framework annotation and @RequestParam is from Spring. QueryParam is from another framework and you are mentioning Spring. @Flao wrote that @RequestParam is from Spring and that should be used in Spring MVC.


1 Answers

Solved with following changes

(a) jQuery Call

 data: {myList: myList},

(b) Controller Method

@RequestMapping(value = "/myApp/path/toController", method = RequestMethod.POST)
public @ResponseBody boolean doSomething(Model model, @RequestParam("myList[]") List<Long> myList)   
{
   System.out.println(myList);
   return true;
}
like image 100
αƞjiβ Avatar answered Nov 29 '22 12:11

αƞjiβ