10

レストサービスを実装しました。

フィルターでリクエストのパスパラメーターを取得しようとしています。

私のリクエストは

/api/test/{id1}/{status}

 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException
    {
         //Way to get the path parameters id1 and status


     }
4

3 に答える 3

24

フィルターで HttpServletRequest をオートワイヤーし、それを使用して情報を取得できます。

@Autowire
HttpServletRequest httpRequest


httpRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)  

will give you map of path params.

例:

リクエストが url/{requestId} のような場合、上記のマップが返されます

0 = {LinkedHashMap$Entry@12596} "requestId" -> "a5185067-612a-422e-bac6-1f3d3fd20809"
 key = "requestId"
 value = "a5185067-612a-422e-bac6-1f3d3fd20809"
于 2018-07-27T06:15:25.973 に答える
5

自分で URI を解析する以外に ServletFilter でそれを行う方法はありませんが、JAX-RS 要求フィルターを使用することにした場合は、パス パラメーターにアクセスできます。

@Provider
public class PathParamterFilter implements ContainerRequestFilter {

    @Override
     public void filter(ContainerRequestContext request) throws IOException {
        MultivaluedMap<String, String> pathParameters = request.getUriInfo().getPathParameters();
        pathParameters.get("status");
        ....
    }
}
于 2014-03-07T16:16:02.197 に答える