Need to do the HTTP equivalent of call-forwarding in your Spring Boot application?

If so, then you've come to the right place. I'll show you how to do it here.

But first, let's talk about the whole concept of forwarding when it comes to the web.

Hangin' out With the 300's

By now you probably know that whenever you hit a URL with your browser, it returns not only some content (like a web page) but also a status code.

That status code is a three-digit number.

The "OK" status code is 200. That means everything is peachy.

It's the status code you got when you accessed this web page.

In fact, usually any status code within the 200's is a good sign.

But if you're in the 400's or 500's, you're in trouble. That means something is wrong.

The 300's are a whole different species. They don't necessarily mean good or bad. They mean you got redirected when you entered your initial URL.

Your call got forwarded, in other words.

Now as a webmaster, you might want to redirect visitors from an old, outdated URL to a new URL. That's perfectly normal.

In that situation, you're going to return a status code of 301 (Moved Permanently) while sending the visitor to the new URL which eventually returns a status code of 200 (OK).

That whole business is called a 301 redirect.

And yes, you can make it happen with Spring Boot.

The Solution

Let's say you're working on a blog powered by Spring Boot. If so, then you and I have something in common because this blog is powered by Spring Boot.

Anyhoo, you've got some old blog content that you need forwarded to new URLs (we have something in common again). You'd like to do that with a 301 redirect.

Head over to your controller class and add some code that looks like this:

	@GetMapping("/2019/{month}/{day}/{slug}")
	public String readPost2019(@PathVariable String month, @PathVariable String day, @PathVariable String slug, HttpServletResponse response) {
	    
	    if ("pattern-like-searching-spring-boot-mongodb".equals(slug)) {
	        response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
	        response.setHeader("Location", "https://careydevelopment.us/blog/spring-boot-and-mongodb-how-to-do-pattern-matching-or-like-searches");
	        response.setHeader("Connection", "close");
	    }
	    
	    String html = getHtml("2019", month, day, slug);
	    return html;
	}

That, by the way, is actual code that I'm using right now to redirect some old content.

Hone in on the code in the if block. That's what you're looking for.

Specifically, the code above uses the HttpServletResponse object to handle the redirect.

Keep in mind: you can include that HttpSerlvetResponse object in any controller method. Just add it as a parameter like you see in the code above and the framework will give it to you.

But now let's focus on what's happening in that if block.

First, the code sets the HTTP response status. In this case, it's an integer specified by the SC_MOVED_PERMANENTLY constant in the HttpServletResponse class.

Press your buzzer if you think you know the value of that integer. 

Collect your winnings if you guessed 301.

But setting the HTTP status code isn't the only thing the code does. The code also sets the URL for the redirect.

It's set with the "Location" header property. You can see it in the second line after the if.

In the code above, that location is hardcoded to a URL on this very website.

The third line in the if block is optional if you're redirecting people to the same domain. I left it here so you can see it in action.

In Action

Want to see that block of code work its magic? That's easy.

Just click on this link: https://careydevelopment.us/2019/01/19/pattern-like-searching-spring-boot-mongodb/

You'll see that you get forwarded to an entirely different URL. It's the value of the URL in the code block above.

And if you check the Developers Tools that you use with your favorite web browser, you'll see the 301 redirect and the 200 response from the target URL.

So it works.

Wrapping It Up

Now you know how to handle 301 redirects within your Spring Boot application.

It's easy, isn't it?

Just apply what you've learned here in your own code and watch the redirecting happen.

Have fun!

Photo by Abby Chung from Pexels