Grizzly的Request.getPathInfo总是返回null?

时间:2013-09-23 15:49:03

标签: java http grizzly

我正在尝试实现自定义Grizzly HttpHandler但是却无法轻易地尝试从传入的请求中提取路径信息。请参阅下面的最小示例:

public class PathInfoTest {
    public static void main(String[] args) throws IOException {
        final HttpServer httpServer = new HttpServer();
        final NetworkListener nl = new NetworkListener(
                "grizzly", "localhost", 8080);
        httpServer.addListener(nl);
        httpServer.getServerConfiguration().addHttpHandler(
                new HandlerImpl(), "/test");   
        httpServer.start();
        System.in.read();
    }

    private static class HandlerImpl extends HttpHandler {
        @Override
        public void service(Request request, Response response)
                throws Exception {

            System.out.println(request.getPathInfo());
            System.out.println(request.getContextPath());
            System.out.println(request.getDecodedRequestURI());
            System.out.println(request.getHttpHandlerPath());
    }
}

我认为这会告诉Grizzly,所有以“/ test”开头的传入请求都应由HandlerImpl处理,这似乎到目前为止工作。但是,在对http://localhost:8080/test/foo执行GET时,此代码会将以下内容打印到stdout

null
/test
/test/foo
null

我主要担心的是第一个null,它应该是路径信息。我希望在此示例中为foo,而不是null。有人可以向我解释一下:

  1. 为什么两个,getHttpHandlerPath()getPathInfo()在此示例中都返回null
  2. 另外,我怀疑后者是第一个的结果,这是对的吗?
  3. 如何在Grizzly的URL中“取消路由”部分?

1 个答案:

答案 0 :(得分:3)

您必须在映射中使用星号(类似于Servlet)才能查看正确的pathInfo值。 例如,请使用以下映射:

httpServer.getServerConfiguration().addHttpHandler(
     new HandlerImpl(), "/test/myhandler/*");

并向http://localhost:8080/test/myhandler/foo/bar

发出请求

,结果将是:

/foo/bar
/test
/test/myhandler/foo/bar
/myhandler