0

I have REST endpoint with multiple paths as following:

@RequestMapping(method = RequestMethod.POST, path = {"/xxx/yyy", "/zzz"}) @ResponseBody public Mono<EpcPain> paymentOrder(@RequestHeader(name = "Timeout", defaultValue = "10000") int timeout, @RequestHeader(name = "X-Request-Id", required = false) String xRequestId) { ... } 

How can I resolve if request path was xxx/yyy or zzz? I do not want to duplicate this endpoint nor pass some params. I am looking for some spring code magic.

4
  • Maybe you could try something like this stackoverflow.com/a/37718400 Commented Feb 11, 2020 at 9:40
  • Will this answer help ? Commented Feb 11, 2020 at 9:40
  • ServletUriComponentsBuilder.fromCurrentRequestUri().toUriString() will get you the uri Commented Feb 11, 2020 at 9:54
  • Thnx R.G, but I do not want to add new dependency because of that. Commented Feb 11, 2020 at 9:59

2 Answers 2

2

org.springframework.web.context.request.RequestContextHolder may be used to get the path

import static org.springframework.web.servlet.HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE; import static org.springframework.web.servlet.HandlerMapping.LOOKUP_PATH; import static org.springframework.web.servlet.HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE; 

and

 @RequestMapping(value = {"/getDetails","/getDetailsMore"}, method = RequestMethod.GET) public String getCustomerDetails(TestFormBean bean) { RequestAttributes reqAttributes = RequestContextHolder.currentRequestAttributes(); System.out.println(reqAttributes.getAttribute(BEST_MATCHING_PATTERN_ATTRIBUTE, 0)); System.out.println(reqAttributes.getAttribute(LOOKUP_PATH, 0)); System.out.println(reqAttributes.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, 0)); return "test"; }} 

All three prints the path.

Here 0 - is request scope and 1 - is session scope.

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

1

You could add ServerHttpRequest as a method argument and then get the URI for the current request using getURI(). It should work for both Spring MVC and Spring WebFlux.

Have a look at the handler methods documentation for details.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.