[英]Spring data rest, java.util.Date in url param?
我有一個名為Check
的簡單POJO。 我有一個簡單的休息庫:
@RepositoryRestResource(collectionResourceRel = "check",path = "check")
public interface RestCheckRepo
extends JpaRepository<Check,Integer> {
public List<Check> findByShopName(@Param("shop") String shop);
public List<Check> findByDateTime(@Param("dt")Date dt);
public List<Check> findByShopNameAndDateTime(@Param("shop")String shop, @Param("dt")Date dt);
public List<Check> findByShopNameAndDateTimeBetween(@Param("shopName") String shop,
@Param("start")Date t1,
@Param("end") Date t2);
}
一切正常!! 但我不知道如何使用java.util.Date
作為@RequestParam
實現請求處理程序。 示例: http:// localhost:8080 / check / search / findByDateTime?dt = {value}
UPDATE請求http:// localhost:8080 / check / search / findByDateTime?dt = 2015-08-10T13:47:30 --->響應:
{
"cause": {
"cause":null,
"message":null
},
"message":"Failed to convert from type java.lang.String to type @org.springframework.data.repository.query.Param java.util.Date for value '2015-10-07T15:04:46Z'; nested exception is java.lang.IllegalArgumentException"
}
@kucing_terbang的解決方案將起作用,否則有一個更簡單的解決方案,您需要這樣做:
public List<Check> findByShopNameAndDateTime(
@Param("shop")String shop,
@DateTimeFormat(your-format-comes-here)@Param("dt")Date dt);
您可以注冊自定義編輯器,以便spring能夠將參數傳遞給參數。 例
@InitBinder
public void dataBinding(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, "dob", new CustomDateEditor(dateFormat, true));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.