Java/Spring boot - Redirect after POST request not working

Viewed 362

I have a simple controller for a post request, after which I would like to redirect to another page. This is the controller:

@Controller
public class NoteController {
    private NoteService noteService;
    private UserService userService;

    public NoteController(NoteService noteService, UserService userService) {
        this.noteService = noteService;
        this.userService = userService;
    }

    @PostMapping("/notes")
    public String postNote(@ModelAttribute Note noteForm, Authentication authentication, Model model) {
        User user = this.userService.getUser(authentication.getName());
        Integer userid = user.getUserId();

        try {
            noteService.createNote(noteForm, userid);
            model.addAttribute("success",true);
            model.addAttribute("message","New note added!");
        } catch (Exception e) {
            model.addAttribute("error",true);
            model.addAttribute("message","System error!" + e.getMessage());
        }

        return "redirect:/result";
    }
}

And after a successful request where I can see that an item is saved in DB, the redirect fails and I get a message:

There was an unexpected error (type=Not Found, status=404). No message available

There is no error in terminal. So, I can't see why is this happening. I am completely new to Spring and Java, so I have no idea what am I doing wrong here?

Update

I have tried with creating a controller for a result template as suggested in answers.

@Controller
@RequestMapping("/result")
public class ResultController {
    @GetMapping()
    public String getResultPage() {
        return "result";
    }
}

Then if I do a redirect from a NoteController like this:

@PostMapping("/notes")
public String postNote(@ModelAttribute Note noteForm, Authentication authentication, Model model) {
    User user = this.userService.getUser(authentication.getName());
    Integer userid = user.getUserId();

    try {
        noteService.createNote(noteForm, userid);
        model.addAttribute("success",true);
        model.addAttribute("message","New note added!");
    } catch (Exception e) {
        model.addAttribute("error",true);
        model.addAttribute("message","System error!" + e.getMessage());
    }

    return "redirect:result";
}

I am redirected to a result template, but the attributes are not being passed:

<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

        <link rel="stylesheet" type="text/css" media="all" th:href="@{/css/bootstrap.min.css}">

        <title>Result</title>
    </head>
    <body class="p-3 mb-2 bg-light text-black">
        <div class="container justify-content-center w-50 p-3" style="margin-top: 5em;">
            <div class="alert alert-success fill-parent" th:if="${success}">
                <h1 class="display-5">Success</h1>
                <span th:text="${message}">Success Message</span>
                <span>Your changes were successfully saved. Click <a th:href="@{/home}">here</a> to continue.</span>
            </div>
            <div class="alert alert-danger fill-parent" th:if="${error}">
                <h1 class="display-5">Error</h1>
                <span>Your changes were not saved</span>
                <span th:text="${message}">Error Message</span>
                <span>Click <a th:href="@{/home}">here</a> to continue.</span>
            </div>
        </div>
    </body>
</html>

How should I pass the attributes to this template?

1 Answers

You have to create an /result endpoint in your controller. It's not possible for spring to autmaticly generate an endpoint.

Related