简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM