[英]How to pass java.util.date in REST urls using Jersey REST cleint
在我的Java
Spring MVC
Web 應用程序中,我使用Jersey
REST 客戶端。 我試圖通過向服務器發送兩個Date
對象來獲取一些數據。 但是我無法在 url 中使用 Date 對象。 恐怕如果我將它們轉換為字符串,我可能無法在我的服務器端獲得確切的時間戳。 我的網址是:
RESTDomain/siteid/{siteid}/pickupdate/{pickupdate}/returndate/{returndate}/pickuplocation/{pickuplocation}/returnlocation/{returnlocation}
因此,有了其中的數據,它看起來像:
/siteid/5/pickupdate/Thu Apr 14 00:00:00 IST 2016/returndate/Fri Apr 29 00:01:00 IST 2016/pickuplocation/1/returnlocation/1
我的控制器將是:
@ResponseBody
@RequestMapping(
value = "/siteid/{siteid}/pickupdate/{pickupdate}/returndate/{returndate}/pickuplocation/{pickuplocation}/returnlocation/{returnlocation}",
method = RequestMethod.GET,
headers = "Accept=application/json"
)
public CarDetailsListHB getDetails(
@ModelAttribute("siteid") int siteId,
@ModelAttribute("pickuplocation") int pickUpLocation,
@ModelAttribute("returndate") Date returnDate,
@ModelAttribute("pickupdate") Date pickupDate,
@ModelAttribute("returnlocation") int returnLocation,
ModelMap model
) {
//logic here
}
是否有任何解決方案。 任何幫助,將不勝感激。 謝謝
您可以將其作為字符串傳遞並轉換為所需的格式。下面的代碼采用字符串並以與傳遞的字符串相同的格式返回 java.util.Date。 檢查以下內容:
//your date as String
String date="Thu Apr 14 00:00:00 IST 2016";
SimpleDateFormat dateformat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
Date returnDate=dateformat.parse(date);//returnDate will have the Date object in same format.
System.out.println(returnDate);
你的@ModelAttribute("returndate") String returnDate
應該是 String 類型。 並在您的控制器方法中
SimpleDateFormat dateformat = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy");
Date newReturnDate=dateformat.parse(returnDate);//newReturnDate will have the Date object in same format.
要修改時間部分,您可以嘗試以下操作:
Calendar cal = Calendar.getInstance();
cal.setTime(newReturnDate);
cal.set(Calendar.HOUR,11 );
cal.set(Calendar.MINUTE,39 );
newReturnDate=cal.getTime();
System.out.println(newReturnDate);
因此 newReturnDate 將具有更新的時間。您需要從字符串“11:39”中獲取 int 值(小時和分鍾部分)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.