Maybe a tiny bug?
yingzhuo opened this issue · 2 comments
yingzhuo commented
Sorry. I don't speak Japanese.
AntPthMatcher with VERSIONED_PATH_PATTERN is very very smart, sir!
But VERSIONED_PATH_PATTERN cannot be a fixed value.
Please check my controller below:
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import java.util.Optional;
@RestController
@RequestMapping("/{version}")
public class TestController {
private static final PathMatcher PATH_MATCHER = new AntPathMatcher();
private static final String VERSIONED_PATH_PATTERN = "/{version:.*}/**";
@GetMapping("/test")
public Map<String, String> test(@PathVariable("version") String version, HttpServletRequest request) {
return Map.of(
"version-from-path-variable", version,
"version-from-path-matcher", getRequestApiVersion(request)
);
}
private static String getRequestApiVersion(HttpServletRequest request) {
return Optional
.ofNullable(PATH_MATCHER.extractUriTemplateVariables(VERSIONED_PATH_PATTERN, request.getRequestURI()))
.map(map -> map.get("version"))
.orElse(null);
}
}
if the spring-boot configuration property (in classpath:application.yaml)
like the
server:
servlet:
context-path: "/" # default value
The code is fine.
but if you change the configuration to your name ("/uich")
the version string will be parsed as "uich".
Thank you all the same, your post in QITA.COM saved my hours!
yingzhuo commented
Thanks again. Good night!