How to set base url for rest in spring boot?

Cover Image for How to set base url for rest in spring boot?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

🌐 How to Set Base URL for REST in Spring Boot? 🚀

So, you want to mix MVC and REST in a single Spring Boot project, eh? And you want to set a base path for all your REST controllers in a single place? No worries, mate! I gotchu covered! 😎

One common approach to achieving this is by utilizing the @RequestMapping annotation in Spring Boot. This nifty little annotation allows you to specify the base path for your REST controllers.

🛠️ Solution

Here's a step-by-step guide to help you set the base URL for REST in your Spring Boot project:

  1. Open your application.properties or application.yml file.

  2. In this file, add the following line:

    server.servlet.context-path=/api

    or

    server: servlet: context-path: /api

    In the above code, /api represents the base path you want to set for your REST controllers. Feel free to replace it with any path of your choice.

  3. Save the file and restart your Spring Boot application. 🔄

Voila! 🎉 Your REST controllers are now accessible at the base path /api.

Now, if you want to access your MVC controllers at a different base path (let's say /whatever), here's what you need to do:

  1. Create a new class, let's call it MvcConfig.

  2. Annotate this class with @Configuration.

  3. Implement the WebMvcConfigurer interface in your MvcConfig class.

  4. Override the addViewControllers method like this:

    @Configuration public class MvcConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/whatever/**").setViewName("forward:/"); } }

    In the above code, /whatever/** represents the base path for your MVC controllers. You can replace it with any path of your choice.

  5. Save the file and restart your Spring Boot application. 🔄

Fantastic! Now your MVC controllers should be easily accessible at the base path /whatever. 🎉

📣 Call-to-Action

There you have it, folks! 💪 Setting the base URL for REST in your Spring Boot project can be as easy as pie. With these simple steps, you'll be able to streamline your REST and MVC controllers without any hassle.

So, why not give it a try and level up your Spring Boot game? 👩‍💻👨‍💻

If you found this guide helpful, don't forget to share it with your fellow developers and spread the knowledge! 🚀

Got any questions or suggestions? Leave a comment below and let's discuss! Happy coding! 😉✨


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello