← Back to list

REST in Pieces: When One Slash Stops Your Spring Boot Endpoint from Working

My articles are open to everyone; non-member readers can read the full article by clicking this link

Ravindu Hirimuthugoda in Javarevisited · 2025-05-01 15:52 · 157 claps · 2.1 min read
#spring-boot #rest-api #spring #api-development #api-design-guidelines
Open on Medium ↗
Wiki topics: FT · Fine-tuning & Adaptation

REST in Pieces: When One Slash Stops Your Spring Boot Endpoint from Working

*My articles are open to everyone; non-member readers can read the full article by clicking this* link

I just implemented a REST endpoint in Spring Boot with the URL mapping /test/. It works perfectly when accessed using /test/, but mysteriously fails when I try /test. Then, when I change the mapping to /test, both /test and /test/ suddenly work. What’s going on?

This tiny slash seems harmless, but it actually plays a big role in how Spring interprets URLs. Let’s dive into the details without the dry tech jargon.

📌 First, What’s the Difference Between /test and /test/?

To humans, they look almost identical. But to a web framework like Spring Boot, they are two different paths:

  • /test is a path with no trailing slash.
  • /test/ is a path with a trailing slash.

Spring, by default, treats these as two distinct URLs unless you configure it otherwise.

🧪 The Case of /test/ Mapping

When you define your controller like this:

@RestController
@RequestMapping("/test/")
public class MyController {
    @GetMapping
    public String hello() {
        return "Hello!";
    }
}

Spring now expects exactly /test/ with the trailing slash.

Calling /test (without the slash) will result in a 404 Not Found, because that route doesn’t exist in Spring’s eyes.

✅ Then Try /test

Let’s say you change your mapping to:

@RestController
@RequestMapping("/test")
public class MyController {
    @GetMapping
    public String hello() {
        return "Hello!";
    }
}

Now things are more flexible. Spring allows both /test and /test/ to match this mapping. This is thanks to a little feature called “trailing slash match”, which is enabled by default in Spring Boot.

Spring is being generous here. It says:

“Hey, if you didn’t specify a trailing slash in the mapping, I’ll try to match both with and without the slash.”

But when you do specify the slash (/test/), Spring assumes you’re serious about it and it’ll only match that exact path.

🔍 In Tech Terms (for the Curious)

Spring Boot uses the PathMatcher under the hood to decide how URLs should match. The trailing slash behavior is governed by the useTrailingSlashMatch setting, which defaults to true.

However, if you include a trailing slash in your @RequestMapping, Spring treats it as exact no extras allowed.

🧠 So, What Should You Do?

If you want your endpoint to be user-friendly and work with both /test and /test/, avoid putting the trailing slash in the @RequestMapping.

✅ Use this:

@RequestMapping("/test")

🚫 Avoid this:

@RequestMapping("/test/")

✨ Final Thoughts

That little slash might seem like a tiny typo, but in web frameworks like Spring Boot, it’s a gatekeeper. By understanding how Spring handles trailing slashes, you can avoid those frustrating 404s and make your APIs more forgiving and user-friendly.

So next time you’re writing a REST endpoint, remember: leave the slash out of the mapping unless you really mean it.

Thanks for reading!!! If you found this helpful, give it a clap (or a few!!!) and feel free to share your thoughts in the comments. 🙌💬


메타데이터
post_id
fbdbb9df2776
slug
rest-in-pieces-when-one-slash-stops-your-spring-boot-endpoint-from-working-fbdbb9df2776
url
https://medium.com/javarevisited/rest-in-pieces-when-one-slash-stops-your-spring-boot-endpoint-from-working-fbdbb9df2776
canonical_url
https://medium.com/javarevisited/rest-in-pieces-when-one-slash-stops-your-spring-boot-endpoint-from-working-fbdbb9df2776
author_url
https://medium.com/@ravindu.18
status
ok
fetched_at
2026-06-09 15:37:30