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

17,107

Solution 1

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");

Solution 2

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

Solution 3

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
Share:
17,107
nand
Author by

nand

EXPERIENCE IN THE AREA OF DEVELOPMENT OF JAVA PROJECTS FOR 5 YEARS. PROFESSIONAL EXPERIENCE IN SOFTWARE DEVELOPMENT INDUSTRY IN HEALTHCARE DOMAIN, WHICH INCLUDES HTML5 BASED DICOM VIEWER IMPLEMENTATION, JAVASCRIPT BASED RESPONSIVE UI AND EXPLORER, JAVA BASED DICOM STORAGE SERVICE. IN HOSPITALITY/TRAVEL DOMAIN, FULLY DEVELOPED PORTAL STAYUNCLE.COM. IT CONSIST REST BASED APPLICATIONS FOR HOTEL SEARCH AS WELL AS CLIENT DATA RETRIEVAL.IMPLEMENTED 3RD PARTY PAYMENT GATEWAY FOR ONLINE PAYMENT. IN EDUCATION INDUSTRY, FULLY DEVELOPED A WEB APPLICATION WHERE MORE THAN 1,00,000 LAKHS PEOPLE CAN GIVE ONLINE TEST AT A SAME TIME.ITS REST BASED APPLICATION WHERE PEOPLE AUTHENTICATION DONE THROUGH TOKEN. EXPERIENCED IN SOFTWARE DEVELOPMENT LIFE CYCLE I.E. WATERFALL AND AGILE, INCLUDING, BUSINESS ANALYSIS, SOFTWARE DEVELOPMENT, TESTING, INTEGRATION, DEPLOYMENT AND IMPLEMENTATION OF CORE PRODUCT. EXTENSIVE EXPERIENCE IN SOFTWARE DESIGN AND DEVELOPMENT. EVALUATING TEST RESULTS INCLUDING DEFECT TRACKING, DEBUGGING/CODE REVIEW. ANALYZING THE APPLICATION REQUIREMENTS AND DESIGNING SPECIFICATIONS. HANDS ON EXPERIENCE IN WORKING INDIVIDUALLY AS WELL AS IN TEAM WITH CUSTOMERS, SMES, OTHER MANAGERS, DEVELOPERS AND OTHER STAKEHOLDERS TO EFFECTIVELY PRIORITIZE ACTIVITIES AND ACHIEVE DEFINED OBJECTIVES. EXECUTION/IMPLEMENTATION OF THE PROJECT IN ACCORDANCE WITH CLIENT’S EXPECTATIONS AND MAKING SURE THAT THE CLIENT IS FULLY SATISFIED WITH THE END RESULTS.

Updated on June 17, 2022

Comments

  • nand
    nand almost 2 years

    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"