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

I am trying to write a rest api in which I pass the date as a URL parameter. Date formatting: dd / MM / yyyy HH: mm ; REST API URL

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

and method "Rest"

     @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 I call this api using a REST client, I get a 404 error . Here is the REST URL

http://localhost:8089/stay/datecheck?dateCheckIn="28/01/2016 19:00"
+4
source share
3 answers

%20. % 2F. ( %20 ​​ % 2F ) . % 3A. URL-: http://www.blooberry.com/indexdot/html/topics/urlencoding.htm

: .

- :

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

.

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

+4

: @PathVariable ( "dateCheckIn" ) @DateTimeFormat (iso = DateTimeFormat.ISO.DATE) dateCheckIn

dateCheckIn @PathVariable, @RequestParam

:

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

, , . . ( ) , "?" . , "=" . . . :

:

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

:

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

:

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

:

var1 = 1
var2 = "abc"

(, , URL-), URL , , , Epoch ( unix time), , 404 - .

: 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

+2

2 .

  • 404 , URL- . , MIME . , /REST, , , .
  • Your next problem is that your date is @QueryParam, not @PathParam. As soon as you fix the encoding problem, you will find that your date will be empty, because this name is not in PathParam
+1
source

Source: https://habr.com/ru/post/1622972/


All Articles