例如:请求/xxx接口需要用路径传参传两个参数
@GetMapping("/xxx/{param}/**")
代码实现
public void xxx(@PathVariable String param, HttpServletRequest request) {
final String path =
request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE).toString();
final String bestMatchingPattern =
request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE).toString();
String arguments = new AntPathMatcher().extractPathWithinPattern(bestMatchingPattern, path);
if (!arguments.isEmpty()) {
param = param + '/' + arguments;
}
//....
}
其中最后得到的param即为接口后的所有参数
👉推荐!!!【腾讯云】爆款2核4G云服务器首年74元/年
👉推荐!!!【腾讯云】1核2G5M轻量应用服务器50元/年
【腾讯云】云数据库低至9.9/年!MySQL7.4元/月
【阿里云】ECS云服务器特惠
【阿里云】服务器首购优惠
如果文章对您有帮助,扫个红包码呗

本文详细介绍了如何在Spring MVC中使用PathVariable从URL路径中提取参数,通过`HttpServletRequest`获取最佳匹配模式,并结合AntPathMatcher实现接口参数的动态组合。
6万+

被折叠的 条评论
为什么被折叠?



