0

I am facing issue in getting attributes after redirecting in external url. I am getting attributes values as null when redirecting to external url where as If I put these method in same webapp It works.

Please suggest what I am doing wrong, or if there is any way, so that I can pass attribute to redirected url and use It.

I don't want attributes to be shown as Queryparams.

sample code is as below.

//Controller in app1

@PostMapping("/redirect")
public RedirectView next(RedirectAttributes redirectAttributes, Model model, HttpServletResponse response, HttpServletRequest request) {
    request.setAttribute("payment",new Payment("1","2","3"));
    redirectAttributes.addAttribute("test", "test");

    redirectAttributes.addFlashAttribute("message", "Successfully changed..");
    redirectAttributes.addFlashAttribute("payment1",new Payment("1","2","3"));

    request.setAttribute(
              View.RESPONSE_STATUS_ATTRIBUTE, HttpStatus.TEMPORARY_REDIRECT);
    RedirectView redirectView = new RedirectView("redirect:http://localhost:8085/redirectedPostToPost", true);
    redirectView.addStaticAttribute("payment",new Payment("1","2","3"));
    return redirectView;
}

//Controller of app2.

@PostMapping("/redirectedPostToPost")
public ModelAndView redirectedPostToPost(@ModelAttribute("message") String message,@ModelAttribute("payment") Payment payment,@ModelAttribute("payment1") Payment payment1, RedirectAttributes redirectAttributes,Model model, HttpServletResponse httpServletResponse, HttpServletRequest request, ModelAndView modelview) {
    logger.info("in redirectedPostToPost:{} : modelview:{}", model, modelview.getModel());
    Map<String, ?> inputFlashMap = RequestContextUtils.getInputFlashMap(request);
    logger.info("flashmap:{}", inputFlashMap);
    return new ModelAndView("about");
}

Thanking you guys in advance.

Vikas Bek
  • 33
  • 7

1 Answers1

0

HTTP support only GET method redirect. here is details why doesn't HTTP have POST redirect

There is a workaround you send data to view and create the FORM in the view you should use all hidden field for data to submit. it will be a white page. You can automatically submit the form redirect URL using the javascript.

Mukhtiar Ahmed
  • 611
  • 4
  • 12
  • If I do so, what happens if on automatic form submit it redirects to another page and on clicking back button of browser to go one step back (previous url) it will redirect again ? wouldn't this issue arise If I do so? – Vikas Bek Apr 11 '20 at 09:31
  • yes, it will redirect again if user click the back button – Mukhtiar Ahmed Apr 11 '20 at 09:58
  • Here it is another possible solution to submit internally using HttpClient https://stackoverflow.com/questions/3324717/sending-http-post-request-in-java – Mukhtiar Ahmed Apr 11 '20 at 10:00