Quantcast
Channel: What's the best way to get the current URL in Spring MVC? - Stack Overflow
Viewing all articles
Browse latest Browse all 8

Answer by Koraktor for What's the best way to get the current URL in Spring MVC?

$
0
0

Instead of using RequestContextHolder directly, you can also use ServletUriComponentsBuilder and its static methods:

  • ServletUriComponentsBuilder.fromCurrentContextPath()
  • ServletUriComponentsBuilder.fromCurrentServletMapping()
  • ServletUriComponentsBuilder.fromCurrentRequestUri()
  • ServletUriComponentsBuilder.fromCurrentRequest()

They use RequestContextHolder under the hood, but provide additional flexibility to build new URLs using the capabilities of UriComponentsBuilder.

Example:

ServletUriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequestUri();builder.scheme("https");builder.replaceQueryParam("someBoolean", false);URI newUri = builder.build().toUri();

Viewing all articles
Browse latest Browse all 8


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>