1

How can I create a single REST API for variable number of path variables such as GET "/ads/{category1}/.../{categoryN}" in Spring?

where number of categories is not fixed

1 Answer 1

1

It's not provided out-of-the-box by Spring, but there is some workaround.

You can map any path like "/ads/**" to your handler method, and then parse categories by splitting the rest of the path by slash:

String path = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
String tailPath = path.substring(5); //skip "/ads/"
String[] categories = tailPath.split("/");
Sign up to request clarification or add additional context in comments.

Comments

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.