我们都知道,通常情况下@PathVariable可以接收请求路径中的参数,配置起来也很简单。

 @RequestMapping("/callrestful/{servId}/{teamId}")
 public Object serviceCall(@PathVariable String servId,
                              @PathVariable String teamId, HttpServletRequest request) 

但如果,参数中包含多个“/”呢?,可以用**占位让的方式,将不带“/”的参数放在首部,如下:  

    private AntPathMatcher antPathMatcher = new AntPathMatcher();

    /**
	 * 描述:通过自定义接口地址访问 <br/>
     */
    @RequestMapping("/callTest/{teamId}/**")
    public String callTest(@PathVariable String teamId,HttpServletRequest request) {
        
        // 获取完整的路径
        String uri = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
        // 获取映射的路径
        String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
        // 截取带“/”的参数
        String customPath = antPathMatcher.extractPathWithinPattern(pattern,uri);

        return uri+"------"+pattern+"------"+customPath;
    }
路径示例:/api/callTest/4b94d/5a6004cf04/63521/16647e69b851 

4b94d(servId) 5a6004cf04/63521/16647e69b851(带/的参数)

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐