Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass date(dd/MM/yyyy HH:mm) as a parameter in REST API

I am trying to write a rest api in which I am passing date as a URL parameter. Date formate is dd/MM/yyyy HH:mm; REST API URL Is

public static final String GET_TestDate = "/stay/datecheck?dateCheckIn={dateCheckIn}";

and Rest Method is

     @RequestMapping(value = HotelRestURIConstants.GET_TestDate, method = RequestMethod.GET)
        public @ResponseBody String getDate(@PathVariable("dateCheckIn")  @DateTimeFormat(iso= DateTimeFormat.ISO.DATE) String dateCheckIn) {
            logger.info("passing date as a param");
            String str="date"+dateCheckIn;
            return str;
        }

but when am calling this api using REST client I am getting 404 error. Here is REST URL

http://localhost:8089/stay/datecheck?dateCheckIn="28/01/2016 19:00"
like image 510
nand Avatar asked Jan 05 '16 18:01

nand


3 Answers

Instead of space, use %20. Instead of slash, you can use %2F. But, you have to decode (transform %20 to space and %2F to slash) after you get the value. Instead of colon, use %3A. You have an URL enconding table here: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

The last hint: don't use quotes.

Try something like:

http://localhost:8089/stay/datecheck?dateCheckIn=28%2F01%2F2016%2019%3A00

Remember to decode it.

Something like: String result = java.net.URLDecoder.decode(url, "UTF-8");

like image 62
Paulo Avatar answered Sep 23 '22 04:09

Paulo


The main problem is here: @PathVariable("dateCheckIn") @DateTimeFormat(iso= DateTimeFormat.ISO.DATE) String dateCheckIn

dateCheckIn should not be @PathVariable but @RequestParam

Let's see the difference:

http://localhost:8089/stay/{path_var}/datecheck?{query_param}=some_value

Path variable is a part of the path, it must be there for the path to map correctly to your method. In the actual call, you never actually specify any name for the variable. Query parameter (or request parameter) is a parameter that occurs after "?" which appears after the path. There you always write the name of a parameter followed by the "=" sign and the value. It might or may not be required. See following example:

Path String:

String GET_TestDate = "/stay/{path_var}/datecheck";

Parameter annotations:

@PathVariable("path_var") Integer var1, @RequestParam("query_param") String

Actual call:

http://localhost:8089/stay/1/datecheck?query_param=abc

Values populated:

var1 = 1
var2 = "abc"

There might be other problems (such as the date format you used in your URL - you shouldn't use quotes and spaces and should URL encode it or change the format to use dashes for example or send time and date in Epoch (unix time) format), but I believe the 404 is because of the wrong Path String and annotations on your method.

More on the topic: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates

like image 25
Wolf Avatar answered Sep 24 '22 04:09

Wolf


You actually have 2 problems.

  • Your 404 is because your URL doesn't match any patterns. This is almost certainly because you didn't MIME encode your date parameter. An actual browser will do this for you but code/REST clients probably won't as they wisely should never mess with your input.
  • Your next problem is that your date is a @QueryParam and not @PathParam. Once you fix the encoding issue you would then discover that your date would be null since there is not PathParam by that name
like image 37
Christian Bongiorno Avatar answered Sep 25 '22 04:09

Christian Bongiorno