如何在Rest Get调用中将Java日期作为路径变量传递 [英] How to pass Java date as path variable in a rest get call

查看:50
本文介绍了如何在Rest Get调用中将Java日期作为路径变量传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现有的api具有以下代码:

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;
}

现在,我创建了一个新的rest服务,该服务将java日期作为路径变量,如下所示:

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/?

现有api中没有使用日期格式.它只是创建一个Java日期,并将查询传递给Db.没有转换.因此,我不确定它将传递给我什么.因此,我所做的就是创建了一个简单的rest服务,该服务返回新的Date().当我运行它时,我得到了响应1530137142067(今天是6月27日).这是什么格式?

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.

推荐答案

如果要使用PathVariable,可以使用以下示例方法:

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;
}

这篇关于如何在Rest Get调用中将Java日期作为路径变量传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆