带有ANT通配符的Spring MVC @GetMapping(“/ v1 / **”):如何获取完整路径?

时间:2018-04-04 21:53:18

标签: java spring spring-mvc

考虑使用@RestController的Spring MVC @GetMapping映射到/v1/**以处理所有子路径。

@RestController
public class MyController {

    @GetMapping(value = "/v1/**", produces = MediaType.TEXT_HTML_VALUE)
    public ModelAndView handle(String fullPathInMethodArg) {
        ...
        String fullPathInLocalVar = ...;
        ...
    }
}

我如何改变完整的路径?

理想情况下,我想将其作为方法中的参数检索:String fullPathInMethodArg

或者,我想将其作为局部变量检索:String fullPathInLocalVar

1 个答案:

答案 0 :(得分:2)

我认为这回答了你的问题:

@RequestMapping("/v1/**")
public void foo(HttpServletRequest request) {
    String fullPath = (String) request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
    String subPath = StringUtils.removeStart(fullPath, "/v1");
    ...
}

Spring 3 RequestMapping: Get path value