Redirect in the Spring Boot framework

In this post I will show you how to redirect or direct the user to another URL within the method of a controller in the Spring Boot framework.

Redirecting to another URL in Spring Boot is very useful when the user does a certain action and we must return it to the main page.

Send to another route in Spring Boot

To make a redirect, simply create the method in the Controller and return a String.

Within the method, return a string with the following format:


Example to redirect in Spring Boot

Let’s see a very simple example. I have my Spring Boot controller called, inside it I must do a redirect within the method called guardarProducto.

To do this, I do the following:

On line 2 I define the method; It is very important to return a String.

Finally on line 4 we redirect to /productos/agregar using redirect:.

When the user calls that method, a redirect to that route will be done, after saving the Producto entity.

Important note:

Do not put the @ResponseBody annotation in the definition of your method, because if you do, the redirect will not work.

Estoy disponible para trabajar en tu proyecto o realizar tu tarea pendiente, no dudes en ponerte en contacto conmigo.
Si el post fue de tu agrado muestra tu apoyo compartiéndolo, suscribiéndote al blog, siguiéndome o realizando una donación.

Suscribir por correo

Ingresa tu correo y recibirás mis últimas entradas sobre programación, open source, bases de datos y todo lo relacionado con informática

Join 496 other subscribers

Categories: Spring Boot


Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: