2016-05-24 2 views
0

後、私はタイプのいくつかのURL持っているすべてを取得:それは変数のパスと考えるべきでは後に、私はすべてをしたい"/something/"で始まるすべてのURLで"/something/some/random/path"春@PathVariable/

@RequestMapping("/something/{path}") 
public MyCustomObj get(@PathVariable("path") String path){ 
    System.out.println(path); // "some/random/path" 
} 

私が知っていますリダイレクトでは可能ですが、必要なものではありません。 私は正規表現を試みたが、それを行うにはどのような方法、または多分いくつかの作業arroundsがあり

@RequestMapping("/spring-web/{path:.*}

を動作するようですしないのですか?

おかげ

答えて

2

私はここで2つの回避策を参照してください。

  1. @RequestMapping("/something/**")とのHttpServletRequestを注入: public MyCustomObj get(HttpServletRequest)、手動では、カスタムHandlerMethodArgumentResolverを使用して上記と同じ操作を行いますrequest.getServletPath()
  2. を使用してパスを解析します。たとえば、カスタム注釈を作成できます。 @MyPathpublic MyCustomObj get(@MyPath String path)

    public class MyPathResolver implements HandlerMethodArgumentResolver { 
    
        @Override 
        public boolean supportsParameter(MethodParameter parameter) { 
         return parameter.hasParameterAnnotation(MyPath.class); 
        } 
    
        @Override 
        public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, 
        NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { 
         return ((ServletWebRequest) webRequest).getRequest().getServletPath().split("/")[2]; 
         //you can do whatever you want here, you can even get a value from your RequestMapping annotation 
         and customize @MyPath value as you want 
        } 
    } 
    

次に、あなたは、このように新しく作成された注釈を注入することができます。あなたの議論を登録することを忘れないでください。

関連する問題