How to pass Java date as path variable in a rest get call

微笑、不失礼 提交于 2021-02-10 12:32:31

问题


There is an existing api which is having the below code :

@GetMapping(value = "/getAmount")
public long getAmount(){

    Date d = new Date();
    long amountFetchedFromDb = callToDatabase(d);
    return amountFetchedFromDb;
}

Now I need to change the functionality as below:

@GetMapping(value = "/getAmount")
public long getAmount(){

    Date d = new Date();
    <CALL TO A NEW  REST API PASSING IT THE DATE d AND THE NEW API 
     WILL MAKE THE DB CALL AND RETURN THE AMOUNT>
    return amount;
}

Now, I have created a new rest service which takes a java date as a path variable as below:

@GetMapping("/getAmount/{dateTo}")
public long getAmount(@PathVariable Date dateTo){

   long amountFetchedFromDb = callToDatabase(d);
   return amountFetchedFromDb;
}

Now i need to test my new service.How to pass the date in the request below:

http://localhost:8080/getAmount/?

There is no date format being used in the existing api. it just creates a java date and passes in the query to Db. No conversion. So, i am not sure what will it pass to me. So, what I did was created a simple rest service which returns new Date(). When I ran it, I got in response 1530137142067(Today is 27June). What format is this?

I hope this is not too confusing. Please let me know if my query is not clear.


回答1:


If you want to use a PathVariable, you can use the example method below:

//You can consume the path .../getAmount/2019-04-25
@GetMapping("/getAmount/{dateTo}")
public long getAmount(@PathVariable("dateTo") @DateTimeFormat(pattern = "yyyy-MM-dd") Date dateTo) {
    long amountFetchedFromDb = callToDatabase(dateTo);
    return amountFetchedFromDb;
}



回答2:


There a couple of ways to achieve this.

Pass in a simple String date as a path variable. Then use SimpleDateFormat to parse the String into a date object. Note here that in this way you'll be dealing with conversion that may fail. Also your clients should be aware of the date format they will be using. Not the very best approach in my opinion but do-able.

Pass in an number defining an epoch and use this to construct your date object. Assuming that you're not interested in very fine precision (nanoseconds etc) you should be fine.

So in case of the first option your get request would look like:

someurl/getAmount/28-06-2018/

For the second:

someurl/getAmount/1530137142067

In my opinion the latter is the best.




回答3:


The value 1530137142067 is the number of milliseconds since the Unix epoch.

Example,

$ date
Wed, Jun 27, 2018  3:28:30 PM
$ date +%s
1530138468

The +%s option asks the date command to return the seconds since the epoch. If you format your value as 1530137142.067 you can see it's very close to what my system displayed.

This is probably the "best" (most portable) way to transmit a timestamp as a parameter, as long as both ends agree as to the timezone. The value returned by the date +%s command is in UTC. For maximum portability, you should use UTC everywhere and convert to the local timezone only on output. Internaly, everything should be UTC.



来源:https://stackoverflow.com/questions/51072142/how-to-pass-java-date-as-path-variable-in-a-rest-get-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!