[英]Spring data rest, java.util.Date in url param?
I have a simple POJO named as Check
. 我有一个名为Check
的简单POJO。 And I have a simple rest repository: 我有一个简单的休息库:
@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);
}
All works fine!! 一切正常!! But I don't know how to implement a request handler using java.util.Date
as a @RequestParam
. 但我不知道如何使用java.util.Date
作为@RequestParam
实现请求处理程序。 Example: http://localhost:8080/check/search/findByDateTime?dt= {value} 示例: http:// localhost:8080 / check / search / findByDateTime?dt = {value}
UPDATE Request http://localhost:8080/check/search/findByDateTime?dt=2015-08-10T13:47:30 ---> Response: 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"
}
Solution of @kucing_terbang will work, otherwise there is a simpler solution, you need do this: @kucing_terbang的解决方案将起作用,否则有一个更简单的解决方案,您需要这样做:
public List<Check> findByShopNameAndDateTime(
@Param("shop")String shop,
@DateTimeFormat(your-format-comes-here)@Param("dt")Date dt);
You can register a custom editor so that spring able to pass the parameter to the argument. 您可以注册自定义编辑器,以便spring能够将参数传递给参数。 example 例
@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.