Mastering SpringBoot Path Variables: Regex, Placeholders, and Suffix Matching

This guide explains how to use regular‑expression path variables, property placeholders, class‑level parameters, and suffix‑based content negotiation in SpringBoot 3.2.5, providing code examples, configuration tips, and practical request illustrations for flexible URL routing.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering SpringBoot Path Variables: Regex, Placeholders, and Suffix Matching

Environment: SpringBoot 3.2.5

1. Regular‑Expression Path Variables

Controller mapping can use {varName:regex} to capture parts of the URL and pass them as method parameters, increasing routing flexibility.

@GetMapping("/{name:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{ext:\\.[a-z]+}")
public Object p1(@PathVariable String name, @PathVariable String version, @PathVariable String ext) {
    return String.format("%s-%s%s", name, version, ext);
}

name matches one or more lowercase letters or hyphens ( [a-z-]+); version matches three numbers separated by dots ( \\d\\.\\d\\.\\d); ext matches a dot followed by one or more lowercase letters ( \\.[a-z]+).

Request example for regex path
Request example for regex path

2. URI Placeholder (${...})

URI patterns can embed ${...} placeholders that are resolved at startup using PropertySourcesPlaceholderConfigurer. This allows external configuration of base URLs.

@GetMapping("${pack.uri}")
public Object uri() {
    // TODO
    return "uri";
}

Define the placeholder in application.yml:

pack:
  uri: /xxxooo
Request example for placeholder
Request example for placeholder

Combination with a static prefix:

@GetMapping("/api${pack.uri}")
public Object uri() { }

SpEL expression can also be used:

@GetMapping("/api${pack.uri}/#{1+2}")
public Object uri() { }
Request example for SpEL
Request example for SpEL

3. Class‑Level Path Parameter

@RestController
@RequestMapping("/users/{type}")
public class PathController {
    @GetMapping("/single/{id}")
    public Object p1(@PathVariable Integer type, @PathVariable Long id) {
        return DATAS.stream()
            .filter(u -> type == u.getType() && id == u.getId())
            .collect(Collectors.toList());
    }
}

The {type} variable is defined at the class level and can be accessed directly in method parameters.

4. Suffix Matching and Content Negotiation

Since Spring MVC 5.3, path patterns ending with .* are no longer matched by default, and the path extension is not used to determine the response media type. The feature was deprecated in 5.2.4 and removed in later versions.

For older versions you can enable it via configuration:

spring:
  mvc:
    contentnegotiation:
      favor-path-extension: true

Or programmatically in Spring 6.x:

@Component
public class PathWebMvcConfigurer implements WebMvcConfigurer {
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
        // Enable path‑extension based negotiation
        configurer.favorPathExtension(true);
    }
}

Example endpoint using suffix pattern:

@GetMapping("/{id}/fmt.*")
public Object p1(@PathVariable Long id) {
    return DATAS.stream()
        .filter(u -> id == u.getId())
        .collect(Collectors.toList());
}
Request example for suffix matching
Request example for suffix matching

When the request ends with .json, the response is JSON; for .xml you need the Jackson XML module:

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-xml</artifactId>
</dependency>

After adding the dependency, Spring automatically registers an XML message converter. Custom formats require implementing a custom HttpMessageConverter.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

backend-developmentSpringBootregexContent negotiationPath VariablesURI Placeholder
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.