Search code examples
javaspringurlhttp-request-parameters

Java Spring - How to take in RequestParam, and remove from url?


Sometimes I will want to pass in an identifier to my website through the url, but I don't want to display this to the user. Is there a simple way to take in a request param but not display it to the user when loading the page?

This is a general idea of how my code is set up currently

@GetMapping("/somePage")
public ModelAndView get(@RequestHeader HttpHeaders headers,
                        @RequestParam(value = "someId", required = false) String someId) {

I know I could theoretically do this on the javascript side, but that seems to require the page to reload or mess with the history.


Solution

  • Generally this is bad practice - if it's passed in the URL, it'll be visible in the user's browser history. POST is probably the best practice here.

    But to answer your actual question:

    Put your custom value into a header and redirect?

    Something along these lines (untested)

    headers.set("X-Custom-Header1", someId);
    headers.set("Location", "/newEndpoint");
    return new ResponseEntity<>(headers, HttpStatus.FOUND);