5

I have a simple POJO named as Check. 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. Example: http://localhost:8080/check/search/findByDateTime?dt={value}

UPDATE Request http://localhost:8080/check/search/findByDateTime?dt=2015-08-10T13:47:30 ---> Response:

{
    "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"
}
1
  • My guess is you need to parse it as java.sql.Date not as java.util.Date Commented Oct 8, 2015 at 10:40

2 Answers 2

18

Solution of @kucing_terbang will work, otherwise there is a simpler solution, you need do this:

public List<Check> findByShopNameAndDateTime(
    @Param("shop")String shop, 
    @DateTimeFormat(your-format-comes-here)@Param("dt")Date dt);
Sign up to request clarification or add additional context in comments.

Comments

2

You can register a custom editor so that spring able to pass the parameter to the argument. 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));
} 

2 Comments

Ummm, where did you add this method? Surely not to the RestCheckRepo interface, right? And there's no controller, since the point of @RepositoryRestResource is to avoid the need for a controller. Maybe inside some @Configuration annotated class? BTW, is this thread-safe? I know that SimpleDateFormat by itself is not.
I would choose to use @ControllerAdvice annotated class. I am not so sure whether it is gonna work if you use @Configuration annotated class. As for the thread safety, looking at the java doc of DataBinder class, I would say it is thread-safe. I mean, if it is not safe, then the doc would state it. But you probably want to test this to make sure of it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.