2013-09-23 2 views
2

사용자 정의 Grizzly HttpHandler을 구현하려고하지만 들어오는 요청에서 경로 정보를 간단하게 추출하는 데 실패했습니다. 아래의 최소한의 예를 참조하십시오 Grizzly의 Request.getPathInfo는 항상 null을 반환합니까?

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()); 
    } 
} 

나는 이것이 URL로 시작 들어오는 모든 요청을 "/ 테스트"지금까지 작동하는 것 같다 HandlerImpl에 의해 처리되어야 함을 그리 즐 말할 거라 생각 했어요. http://localhost:8080/test/foo에 GET을 수행 할 때 그러나,이 코드는 stdout에 다음과 같은 인쇄 :

null 
/test 
/test/foo 
null 

내 주요 관심사는 경로 정보를해야 첫 null입니다. 이 예의 경우 foo이고, null이 아닙니다. 누군가가 나에게 설명 할 수 :

  1. 이유는 모두,이 예에서는 getHttpHandlerPath()getPathInfo() 반환 null?
  2. 또한 나는 후자가 첫 번째의 결과라고 의심합니다. 맞습니까?
  3. Grizzly에서 URL의 "라우트되지 않은"부분에 손을 대려면 어떻게해야합니까?
+0

http://stackoverflow.com/questions/3745275/ how-come-request-getpathinfo-in-service-method-returns-null – gavenkoa

+1

@gavenkoa 링크 된 질문은 서블릿에 관한 것이지만,이 질문은 서블릿과 관련이 없습니다. – Waldheinz

답변

3

올바른 pathInfo 값을 보려면 매핑에서 별표 (서블릿과 유사)를 사용해야합니다.

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

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

에 요청을하고 결과가 될 것입니다 : 예를 들어 다음과 같은 매핑을 사용하시기 바랍니다 들어

/foo/bar 
/test 
/test/myhandler/foo/bar 
/myhandler 
+0

덕분에,이 트릭을 않습니다. 이것은 분명히 문서의 일부가되어야합니다. – Waldheinz

+0

이 매핑은 서블릿 매핑과 일치하지만 분명히하기 위해 addHttpHandler() 메소드를 더 잘 문서화해야한다는 데 동의합니다. 의견을 주셔서 감사합니다 :) – alexey