0

I have a very odd situation. I have an API endpoint which has to accept requests to list all the resources available for the given endpoint and list a specific resource. The endpoint looks like this :

/v1/patients

I can call this without passing any path variables and it will list all patient records. Here comes the special case - If I want to fetch only one patient, I have to pass in the Patient number as path variable - and My patient number looks something like this - 2019/patientname/October/27 - and that is the problem.

when I pass the above number as path variable, I will not get the resource I wanted, instead Spring considers the slashes "/" as separators.

Example :

/v1/patients/2019/patientname/October/27

And I am getting the response like given path is not found - of course there is no path like that.

Is there a possibility of Ignoring all the slashes in between this one Patient number?

Edit : There is no possibility of changing the Patient number as it is part of a very old and legacy code-base, on which the whole system is running.

Edit 2 - URL encoding cannot be used in this situation - this patient number has to be passed in as it is - Situation is very insane.

  • Possible duplicate of [Java URL encoding of query string parameters](https://stackoverflow.com/questions/10786042/java-url-encoding-of-query-string-parameters) – Raedwald Oct 04 '19 at 13:25
  • Sorry but URL Encoding is of no help to me in this special situation :( – quiet_wolf_008 Oct 04 '19 at 13:33

1 Answers1

1

You could try something like:

@GetMapping("/v1/patients/{year}/{name}/{month}/{day}")
public ResponseEntity<Patient> getPatient(@PathVariable Integer year,
                                          @PathVariable String name,
                                          @PathVariable String month,
                                          @PathVariable Integer day) {

    String patientNumber = year + "/" + name + "/" + month + "/" + day;
    ...
}

And here's something that may also work:

@GetMapping("/v1/patients/**")
public ResponseEntity<Patient> getPatient(HttpServletRequest request) {
    String patientNumner = (String) 
            request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    ...
}
cassiomolin
  • 124,154
  • 35
  • 280
  • 359