Reputation: 1317
I need to send data to Spring MVC controller by ajax. But controller doesn't work if I send more than one parameter.
Controller method:
@Timed
@RequestMapping(value = "saveee", method = RequestMethod.POST)
@ResponseBody
public JsonResultBean saveTicketTemplate(@RequestBody TicketTemplateFieldBean fieldBean, Long id) throws IOException {
//TODO smth
return JsonResultBean.success();
}
With this ajax code all works perfectly:
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: '/organizer/api/saveee',
data: JSON.stringify(fieldBean.data),
success: function(result) {
//TODO
}
})
But if I change data parameters, then controller doesn't get request even.
data: ({'fieldBean': JSON.stringify(fieldBean.data), 'id': id})
What I'm doing wrong?
Upvotes: 3
Views: 7987
Reputation: 1643
That won't work. First lets clarify the difference between @RequestBody and @RequestParam.
The @RequestBody method parameter annotation should bind the json value in the HTTP request body to the java object by using a HttpMessageConverter. HttpMessageConverter is responsible for converting the HTTP request message to an assoicated java object. Source
And Use the @RequestParam annotation to bind request parameters to a method parameter in your controller. Source
Coming to you question... With first ajax request you are sending JSON to your controller not request parameters, so @RequestBody is OK.
In the second ajax request also you are sending JSON but with two fields (fieldBean and id). Since @RequestBody annotated parameter is expected to hold the entire body of the request and bind to one object. You should modify the Java Object(ie TicketTemplateFieldBean) to hold id field also. This will work if you have only one argument in the controller.
Then, how to have second argument?
You cannot use two @RequestBody like :
public JsonResultBean saveTicketTemplate(@RequestBody TicketTemplateFieldBean fieldBean, @RequestBody Long id).
as it can bind to a single object only (the body can be consumed only once), You cannot pass multiple separate JSON objects to a Spring controller. Instead you must Wrap it in a Single Object.
So your solution is to pass it as Request parameter- @RequestParam, or as a path variable - @PathVariable. Since @RequestParam and @ModelAttribute only work when data is submitted as request parameters. You should change your code like this:
@Timed
@RequestMapping(value = "saveee", method = RequestMethod.POST)
@ResponseBody
public JsonResultBean saveTicketTemplate(@RequestBody TicketTemplateFieldBean fieldBean, @RequestParam("id") Long id) throws IOException {
//TODO smth
return JsonResultBean.success();
}
And change you request URL as follows:
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: '/organizer/api/saveee?id=10',
data: JSON.stringify(fieldBean.data),
success: function(result) {
//TODO
}
})
You can use @PathVariable like this:
@Timed
@RequestMapping(value = "saveee/{id}", method = RequestMethod.POST)
@ResponseBody
public JsonResultBean saveTicketTemplate(@RequestBody TicketTemplateFieldBean fieldBean, @PathVariable("id") Long id) throws IOException {
//TODO smth
return JsonResultBean.success();
}
And change you request URL as follows:
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: '/organizer/api/saveee/10',
data: JSON.stringify(fieldBean.data),
success: function(result) {
//TODO
}
})
Upvotes: 6
Reputation: 3631
You're not passing valid data to the controller. Try something like this.
$.ajax({
type: 'POST',
dataType: 'json',
contentType: 'application/json',
url: '/organizer/api/saveee',
data: JSON.stringify({
fieldBean: JSON.stringify(fieldBean.data),
id: id
}),
success: function(result) {
//TODO
}
})
Upvotes: 0
Reputation: 2183
To convert parameter to the method arguments you have to use @RequestParam, so the code should be modified like this:
Controller :
@Timed
@RequestMapping(value = "saveee", method = RequestMethod.POST)
@ResponseBody
public JsonResultBean saveTicketTemplate(@RequestParam TicketTemplateFieldBean fieldBean, @RequestParam Long id) throws IOException {
//TODO smth
return JsonResultBean.success();
}
Upvotes: 0