Spring数据休息,url param中的java.util.Date?

Pro*_*mer 5 java rest spring

我有一个名为的简单POJO Check.我有一个简单的休息库:

@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);


}
Run Code Online (Sandbox Code Playgroud)

一切正常!! 但我不知道如何使用java.util.Dateas 实现请求处理程序@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"
}
Run Code Online (Sandbox Code Playgroud)

Jai*_*o99 16

@kucing_terbang的解决方案将起作用,否则有一个更简单的解决方案,您需要这样做:

public List<Check> findByShopNameAndDateTime(
    @Param("shop")String shop, 
    @DateTimeFormat(your-format-comes-here)@Param("dt")Date dt);
Run Code Online (Sandbox Code Playgroud)