I try to make an AJAX query to my controller in Spring MVC.
My action code is:
@RequestMapping(value = "events/add", method = RequestMethod.POST) public void addEvent(@RequestParam(value = "start_date") String start_date, @RequestParam(value = "end_date") String end_date, @RequestParam(value = "text") String text, @RequestParam(value = "userId") String userId){ //some code }
My Ajax query is:
$.ajax({ type: "POST", url:url, contentType: "application/json", data: { start_date: scheduler.getEvent(id).start_date, end_date: scheduler.getEvent(id).end_date, text: scheduler.getEvent(id).text, userId: userId }, success:function(result){ //here some code } });
But I got an error:
Required String parameter ''start_date is not present
Why? As I know I presented it like (@RequestParam(value = "start_date") String start_date
UDP
Now I give 404 My class to take data
public class EventData { public String end_date; public String start_date; public String text; public String userId; //Getters and setters }
My js AJAX call is:
$.ajax({ type: "POST", url:url, contentType: "application/json", // data: eventData, processData: false, data: JSON.stringify({ "start_date": scheduler.getEventStartDate(id), "end_date": scheduler.getEventEndDate(id), "text": scheduler.getEventText(id), "userId": "1" }),
And controller action:
@RequestMapping(value = "events/add", method = RequestMethod.POST) public void addEvent(@RequestBody EventData eventData){ }
And JSON data is:
end_date: "2013-10-03T20:05:00.000Z" start_date: "2013-10-03T20:00:00.000Z" text: "gfsgsdgs" userId: "1"
The spring boot error Required string parameter is not present occurs when the request parameter is not stated in the request url and the request parameter is configured as mandatory. The controller method needs to provide a value for the request parameter.
MissingServletRequestParameterException. public MissingServletRequestParameterException(String parameterName, String parameterType, boolean missingAfterConversion) Constructor for use when a value was present but converted to null .
1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.
Spring @RequestParam @RequestParam is a Spring annotation used to bind a web request parameter to a method parameter. It has the following optional elements: defaultValue - used as a fallback when the request parameter is not provided or has an empty value. name - name of the request parameter to bind to.
On the server side you expect your request parameters as query strings but on client side you send a json object. To bind a json you will need to create a single class holding all your parameters and use the @RequestBody annotation instead of @RequestParam.
@RequestMapping(value = "events/add", method = RequestMethod.POST) public void addEvent(@RequestBody CommandBean commandBean){ //some code }
Here is a more detailed explanation.
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