0

I have the following call

$.getJSON("/svrBooking/json/getEmployeesByManager",{
            manager: "Paul Walker",
            endDate: endDate, --> "Thu May 28 16:52:25 BST 2015"
            startDate: startDate, --> "Thu Apr 30 16:52:25 BST 2015"
            ajax: 'true'
            }, function (result) {...});

This is calling the following Java code

@RequestMapping(value="/getEmployeesByManager" , method = RequestMethod.GET)
    public @ResponseBody ReservationsCount
    getEmployeesByManager(Model model,@RequestParam String manager,@RequestParam Date startDate, @RequestParam Date endDate) throws ParseException{...}

I am getting the following error

GET 'http://localhost:8080/svrBooking/json/getEmployeesByManager?manager=Paul+Walker&endDate=Thu+May+28+16%3A52%3A25+BST+2015&startDate=Thu+Apr+30+16%3A52%3A25+BST+2015&ajax=true'   400 bad request

the response says

The request sent by the client was syntactically incorrect.

This only started happening when i passed in the Date objects to the getJSON call. If i change the dates to strings everything works fine. Does anyone know what is going on here?

Hip Hip Array
  • 4,665
  • 11
  • 49
  • 80
  • 1
    Different implementations of Date represent date and time differently... You can try http://www.joda.org/joda-time/ jodaTime... Otherwise parse the String to a Date... – Danielson Jun 26 '15 at 08:03
  • but i already have the startDate and endDate to a Date (e.g. startDate = new Date();) so it should not be a string – Hip Hip Array Jun 26 '15 at 08:07

2 Answers2

1

Have a look at this SO question.
Spring does not know how to parse Date parameters. You have to add the @DateTimeFormat annotation after @RequestParam to tell Spring which format to expect. For instance:

@RequestParam @DateTimeFormat("MMddyyyy") Date startDate

In your Javascript API call you must then pass the date in the format you specified.

You should also be able to use the ISO format (I have not tested it) as:

@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date startDate

Then in your Javascript API call you should format the dates using toISOString:

$.getJSON("/svrBooking/json/getEmployeesByManager",{
            manager: "Paul Walker",
            endDate: endDate.toISOString(), --> "2015-06-26T08:38:26.175Z"
            startDate: startDate.toISOString(), --> "2015-06-26T08:38:45.304Z"
            ajax: 'true'
            }, function (result) {...});
Community
  • 1
  • 1
mziccard
  • 2,158
  • 9
  • 17
0

Try using surrounding the data variables by quotes like so:

$.getJSON("/svrBooking/json/getEmployeesByManager",{
            "manager": "Paul Walker",
            "endDate": endDate,
            "startDate": startDate,
            "ajax": 'true'
            }, function (result) {...});
Ahs N
  • 8,233
  • 1
  • 28
  • 33