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.
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.
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.
@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.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With