Search code examples
spring-mvcspring-cloudnetflix-zuul

Spring MVC UI service form submission behind Spring Cloud Zuul


The problem is when a form POST happens and then the controller does a redirect it seems that the redirect does not know it is behind Zuul.

Form:

@RequestMapping(value = "/create-something", method = RequestMethod.GET)
public String getForm(Model model, @CookieValue(value = "XSRF-TOKEN", defaultValue = "none", required=true) String token) {

    model.addAttribute("title", "Create New Something");

    model.addAttribute("_csrf", token);

    return "views/create-something";

}

POST:

@RequestMapping(value = "/create-something", method = RequestMethod.POST)
public String postForm(Model model, @ModelAttribute Something something) {

    SomethingClient.createSomething(something);

    return "redirect:" + something.getName() + "/";

}

This setup will result in the redirect trying to hit the internal IP of the UI service not the public Zuul URL.


Solution

  • After trying different approaches I finally settled on a direct URL. Not sure if there is a better solution but this does work and resolves the above problem.

    New POST:

    @RequestMapping(value = "/create-something", method = RequestMethod.POST)
    public String postForm(Model model, @ModelAttribute Something something) {
    
        SomethingClient.createSomething(something);
    
        return "redirect:http://domain.com/ui/" + something.getName() + "/";
    
    }
    

    Another thought would be an interceptor. That felt like added complexity times a large number of UI services.