Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 402
T Anna Avatar asked Nov 16 '25 17:11

T Anna


1 Answers

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;
}
like image 129
David Jesus Avatar answered Nov 18 '25 07:11

David Jesus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!