简体   繁体   中英

JAVA Rest two dates Path param or Query param

I have a rest service which validates date now i need to modify it to take two dates, but i don't know if to use @PathParam or @QueryParam and if i can insert it between two @PathParam
This it the original code :

 @Path("isDateValid/{date}/{itemId}")
    public boolean isDateValid(@PathParam("date") Date date, @PathParam("itemId") Long itemId) {

Should i do like this :

 @Path("isDateValid/{startDate}/{endDate}/{itemId}")
    public boolean isDateValid(@PathParam("startDate") Date startDate, @PathParam("endDate") Date endDate, @PathParam("itemId") Long itemId) {

Date Class is cannot serialize using JAX-RS as it is not a simple type. You need to develop the same using MessageBodyReader/Writer . Click Here for more

Or you can use some third party stuff to configure to get it done.

Click Here for more

If you do not want to use third party stuff, I suggest you define a format for the text-date. You can use the SimpleDateFormat class (avoid the space). The you can use the following code.

@Path("isDateValid/{itemId}")
public boolean isDateValid(@PathParam("itemId") Long itemId) {
    @QueryParam("begin") String sBegin; 
    @QueryParam("end") String sEnd;

    SimpleDateFormat sdf = new SimpleDateFormat(/* Your patern, for example "yyMMddHHmmssZ"*/);

    Date dBegin = sdf.parse(sBegin);
    Date dEnd = sdf.parse(sEnd);

/*
...
*/
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM