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.

2

1 Answer 1

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);
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This works exactly the way my boss wants it - you saved at least 100 days of mine! :)

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.