In the following Spring 3.1 action, I've to do some stuff and add attribute to a POST request, and then redirect it to external URL through POST (I can't use GET).
@RequestMapping(value = "/selectCUAA", method = RequestMethod.POST)
public ModelAndView selectCUAA(@RequestParam(value="userID", required=true) String cuaa, ModelMap model) {
//query & other...
model.addAttribute(PARAM_NAME_USER, cuaa);
model.addAttribute(... , ...);
return new ModelAndView("redirect:http://www.externalURL.com/", model);
}
But with this code the GET method is used (the attributes are appended to http://www.externalURL.com/). How can I use the POST method? It's mandatory from the external URL.
There are two ways you can do this. Two using RedirectView object. The return type of the method should be ResponseEntity<Void>. STEP2: Build response entity object with 302 http status code and the external URL.
We can use a name such as a redirect: http://localhost:8080/spring-redirect-and-forward/redirectedUrl if we need to redirect to an absolute URL.
The sendRedirect() method of HttpServletResponse interface can be used to redirect response to another resource, it may be servlet, jsp or html file. It accepts relative as well as absolute URL. It works at client side because it uses the url bar of the browser to make another request.
getValue(); System. out. println("redirectURL: " + redirectURL); return true; } return false; } }) . build(); HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(httpClient); RestTemplate restTemplate = new RestTemplate(factory); .......
Like @stepanian said, you can't redirect with POST. But there are few workarounds:
html:
<form name="myRedirectForm" action="https://processthis.com/process" method="post">
<input name="name" type="hidden" value="xyz" />
<input name="phone" type="hidden" value="9898989898" />
<noscript>
<input type="submit" value="Click here to continue" />
</noscript>
</form>
<script type="text/javascript">
$(document).ready(function() {
document.myRedirectForm.submit();
});
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With