Search code examples
springspring-mvcspring-securityspring-bootthymeleaf

Spring redirect on POST


This is my Update User Method:

@ResponseBody
@Transactional
@RequestMapping(value = "/profile/edit/{id}", method = RequestMethod.POST)
public String updateUser(@PathVariable("id") final Integer id, String firstname, String lastname, final RedirectAttributes redirectAttributes) {

    respository.updateFirstname(id,firstname);
    respository.updateLastname(id, lastname);

    redirectAttributes.addFlashAttribute("message", "Successfully changed..");
    return "redirect:/profile";
}

All worked fine. Also the update in the Database. But the redirect is just a String and do not change the Path. Can someone tell me why?


Solution

  • The problem is in the @ResponseBody annotation. Once it is removed, the redirection should work as expected. By using it, you override the default behavior of Spring MVC and returned value is treated as a raw response.