0

I have a URL which has date in the format like "Unable to load rs/Service/Store/Grantor/122/5801/DUE/10/30/2017/ /true?request.preventCache=1562353357306 status: 404" in which 10/30/2017 is a date in the java code it has

@GET
@Path("/dd/{sp}/{rpt}/{ter}/{date}/{grant}/{refresh}")
@Produces(MediaType.APPLICATION_JSON)

public List<ReportPeriodBean> getApprovals(@PathParam("sp") String sp,
        @PathParam("rpt") String rpt, @PathParam("ter") String ter,
        @PathParam("date") String date,
        @PathParam("grant") String grant, @PathParam("refresh") boolean refresh) throws Exception {

How should I allow my URL to go as a right date format and allow controler to take care of the rest is there anyway in spring?

Juke
  • 1,306
  • 2
  • 11
  • 27
  • Are you asking for a strategy to embed a date within a URL? If so, use the standard [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format: YYYY-MM-DD such as `2019-01-23`. `LocalDate.now().toString()` & `LocalDate.parse( "2019-01-23" )`. – Basil Bourque Jul 05 '19 at 22:11

1 Answers1

2

To match the URL in your description, your best bet is going to be to escape the different date components as Month, Day, and Year. Then inside the method, you can piece them back together to become a Date object.

To capture them all as one Date type will run against the URL structure, where it can't tell the difference between the 'slashes' in the Date vs 'slashes' differentiating different URL parameters. Provided you don't want to switch to ISO-8601 representation for the date and don't want to %-encode the slashes to %2F or use Query Strings, etc.

Something like this should work:

@GET
@Path("/dd/{sp}/{rpt}/{ter}/{month}/{day}/{year}/{grant}/{refresh}")
@Produces(MediaType.APPLICATION_JSON)
public List<ReportPeriodBean> getApprovals(@PathParam("sp") String sp,
        @PathParam("rpt") String rpt, 
        @PathParam("ter") String ter,
        @PathParam("month") int month,
        @PathParam("day") int day,
        @PathParam("year") int year,
        @PathParam("grant") String grant, 
        @PathParam("refresh") boolean refresh) {

    LocalDate date = LocalDate.of(year, month, day);
    // Now use the date however you like

}

This will enable you to keep your URL in this syntax that seems preferred:

rs/Service/Store/Grantor/122/5801/DUE/10/30/2017/grantValue/true?request.preventCache=1562353357306

Cuga
  • 17,668
  • 31
  • 111
  • 166
  • or can I convert the request to POST and use requestbody params? – Juke Jul 05 '19 at 19:26
  • If your requirements can support that, it'd possibly be preferred that way. There are a lot of parameters being passed to this method already, and that'd greatly simplify matters. The "GET" vs "POST" question should focus more on the use of the method. Is this returning data in a read-only manner that's not creating new records? Then GET is probably a better choice. POST methods are generally used to create/update data. – Cuga Jul 05 '19 at 19:49
  • 1
    @Juke you also should take a look at this answer: https://stackoverflow.com/a/56920221/101095 . Something like this may work, though I haven't verified it: `@PathParam @DateTimeFormat(pattern = "MM/dd/yyyy") Date date` – Cuga Jul 08 '19 at 00:56