2013-07-23 70 views
0

我已經看到類似的問題和解決方案,但仍然無法弄清楚我的解決方案。Jetty + Guice + Jersey給404訪問資源

我以編程方式啓動Jetty服務器,並使用Guice將Jersey鉤入它。當我嘗試訪問我的球衣資源,我得到404

public class TestService { 

    public static void main(String[] args) throws Exception { 
     Server server = new Server(8080); 

     ServletContextHandler context = new ServletContextHandler(); 
     context.setContextPath("/"); 
     server.setHandler(context); 

     final Injector injector = Guice.createInjector(new MyServletModule()); 
     FilterHolder guiceFilter = new FilterHolder(injector.getInstance(GuiceFilter.class)); 
     context.addFilter(guiceFilter, "/*", EnumSet.allOf(DispatcherType.class)); 
     context.addEventListener(new GuiceServletContextListener() { 
      @Override 
      protected Injector getInjector() { 
       return injector; 
      } 
     }); 

     server.start(); 
     server.join(); 
    } 
} 

class MyServletModule extends ServletModule { 
    @Override 
    protected void configureServlets() { 
     bind(GuiceContainer.class); 

     Map<String, String> parameters = Maps.newHashMap(); 
     parameters.put(PackagesResourceConfig.PROPERTY_PACKAGES, "com.test"); 
     parameters.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true"); 
     serve("/*").with(GuiceContainer.class, parameters); 
    } 
} 

我的資源:

@Singleton 
@Path("/") 
public class MyResource { 

    @GET 
    public String sayhello() { 
     return "hello"; 
    } 
} 

我可以看到,資源被加載在日誌中:

INFO: Root resource classes found: 
    class com.test.MyResource 

但我在​​上收到404。我也嘗試將資源映射到其他網址,fx /web,但它仍然是404

,完整的日誌:

2013-07-23 11:06:07,993 [main] DEBUG org.eclipse.jetty.util.log - Logging to org.slf4j.impl.Log4jLoggerAdapter(org.eclipse.jetty.util.log) via org.eclipse.jetty.util.log.Slf4jLog 
2013-07-23 11:06:08,114 [main] DEBUG org.eclipse.jetty.util.component.Container - Container [email protected] + [email protected]:8080 as connector 
2013-07-23 11:06:08,131 [main] DEBUG org.eclipse.jetty.util.component.Container - Container [email protected] + o.e.j.s.ServletContextHandler{/,null} as handler 
2013-07-23 11:06:08,425 [main] DEBUG org.eclipse.jetty.servlet.ServletHandler - filterNameMap={com.google.inject.servlet.GuiceFilter-1346515=com.google.inject.servlet.GuiceFilter-1346515} 
2013-07-23 11:06:08,426 [main] DEBUG org.eclipse.jetty.servlet.ServletHandler - pathFilters=[[/*]/[]==31=>com.google.inject.servlet.GuiceFilter-1346515] 
2013-07-23 11:06:08,426 [main] DEBUG org.eclipse.jetty.servlet.ServletHandler - servletFilterMap={} 
2013-07-23 11:06:08,426 [main] DEBUG org.eclipse.jetty.servlet.ServletHandler - servletPathMap=null 
2013-07-23 11:06:08,427 [main] DEBUG org.eclipse.jetty.servlet.ServletHandler - servletNameMap={} 
2013-07-23 11:06:08,429 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - starting [email protected] 
2013-07-23 11:06:08,429 [main] INFO org.eclipse.jetty.server.Server - jetty-8.1.0.RC5 
2013-07-23 11:06:08,443 [main] DEBUG org.eclipse.jetty.util.component.Container - Container [email protected] + qtp22355327{8<=0<=0/254,-1} as threadpool 
2013-07-23 11:06:08,444 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - starting o.e.j.s.ServletContextHandler{/,null} 
2013-07-23 11:06:08,453 [main] DEBUG org.eclipse.jetty.util.component.Container - Container [email protected] + com.google.inject.servlet.GuiceFilter-1346515 as filter 
2013-07-23 11:06:08,453 [main] DEBUG org.eclipse.jetty.util.component.Container - Container [email protected] + [/*]/[]==31=>com.google.inject.servlet.GuiceFilter-1346515 as filterMapping 
2013-07-23 11:06:08,454 [main] DEBUG org.eclipse.jetty.util.component.Container - Container o.e.j.s.ServletContextHandler{/,null} + [email protected] as handler 
2013-07-23 11:06:08,454 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - starting [email protected] 
2013-07-23 11:06:08,454 [main] DEBUG org.eclipse.jetty.servlet.ServletHandler - filterNameMap={com.google.inject.servlet.GuiceFilter-1346515=com.google.inject.servlet.GuiceFilter-1346515} 
2013-07-23 11:06:08,454 [main] DEBUG org.eclipse.jetty.servlet.ServletHandler - pathFilters=[[/*]/[]==31=>com.google.inject.servlet.GuiceFilter-1346515] 
2013-07-23 11:06:08,454 [main] DEBUG org.eclipse.jetty.servlet.ServletHandler - servletFilterMap={} 
2013-07-23 11:06:08,454 [main] DEBUG org.eclipse.jetty.servlet.ServletHandler - servletPathMap=null 
2013-07-23 11:06:08,454 [main] DEBUG org.eclipse.jetty.servlet.ServletHandler - servletNameMap={} 
2013-07-23 11:06:08,454 [main] DEBUG org.eclipse.jetty.server.handler.AbstractHandler - starting [email protected] 
2013-07-23 11:06:08,454 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED [email protected] 
2013-07-23 11:06:08,455 [main] DEBUG org.eclipse.jetty.server.handler.AbstractHandler - starting o.e.j.s.ServletContextHandler{/,null} 
2013-07-23 11:06:08,455 [main] INFO org.eclipse.jetty.server.handler.ContextHandler - started o.e.j.s.ServletContextHandler{/,null} 
2013-07-23 11:06:08,455 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - starting com.google.inject.servlet.GuiceFilter-1346515 
2013-jul-23 11:06:08 com.sun.jersey.api.core.PackagesResourceConfig init 
INFO: Scanning for root resource and provider classes in the packages: 
    com.test 
2013-jul-23 11:06:08 com.sun.jersey.api.core.ScanningResourceConfig logClasses 
INFO: Root resource classes found: 
    class com.test.MyResource 
2013-jul-23 11:06:08 com.sun.jersey.api.core.ScanningResourceConfig init 
INFO: No provider classes found. 
2013-jul-23 11:06:08 com.sun.jersey.server.impl.application.WebApplicationImpl _initiate 
INFO: Initiating Jersey application, version 'Jersey: 1.12 02/15/2012 04:51 PM' 
2013-07-23 11:06:09,219 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED com.google.inject.servlet.GuiceFilter-1346515 
2013-07-23 11:06:09,219 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED o.e.j.s.ServletContextHandler{/,null} 
2013-07-23 11:06:09,219 [main] DEBUG org.eclipse.jetty.server.handler.AbstractHandler - starting [email protected] 
2013-07-23 11:06:09,219 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - starting qtp22355327{8<=0<=0/254,-1} 
2013-07-23 11:06:09,221 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED qtp22355327{8<=1<=8/254,0} 
2013-07-23 11:06:09,222 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - starting [email protected]:8080 
2013-07-23 11:06:09,258 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - starting null/null 
2013-07-23 11:06:09,260 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED PooledBuffers [0/[email protected],0/[email protected],0/[email protected]]/PooledBuffers [0/[email protected],0/[email protected],0/[email protected]] 
2013-07-23 11:06:09,260 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - starting org.eclipse.j[email protected]20c906 
2013-07-23 11:06:09,279 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED org.eclipse.j[email protected]20c906 
2013-07-23 11:06:09,279 [qtp22355327-10 Selector0] DEBUG org.eclipse.jetty.io.nio - Starting Thread[qtp22355327-10 Selector0,5,main] on [email protected] 
2013-07-23 11:06:09,280 [main] INFO org.eclipse.jetty.server.AbstractConnector - Started [email protected]:8080 
2013-07-23 11:06:09,280 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED [email protected]:8080 
2013-07-23 11:06:09,280 [main] DEBUG org.eclipse.jetty.util.component.AbstractLifeCycle - STARTED [email protected] 
2013-07-23 11:06:16,602 [qtp22355327-10 Selector0] DEBUG org.eclipse.jetty.io.nio - created [email protected]{l(/127.0.0.1:61292)<->r(/127.0.0.1:8080),d=false,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0}-{[email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} 
2013-07-23 11:06:16,604 [qtp22355327-16] DEBUG org.eclipse.jetty.http.HttpParser - filled 0/0 
2013-07-23 11:06:16,604 [qtp22355327-10 Selector0] DEBUG org.eclipse.jetty.io.nio - created [email protected]{l(/127.0.0.1:61293)<->r(/127.0.0.1:8080),d=false,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=0}-{[email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} 
2013-07-23 11:06:16,604 [qtp22355327-14] DEBUG org.eclipse.jetty.http.HttpParser - filled 0/0 
2013-07-23 11:06:16,634 [qtp22355327-17] DEBUG org.eclipse.jetty.http.HttpParser - filled 328/328 
2013-07-23 11:06:16,641 [qtp22355327-17 - /] DEBUG org.eclipse.jetty.server.Server - REQUEST/on [email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=14,c=0},r=1 
2013-07-23 11:06:16,641 [qtp22355327-17 - /] DEBUG org.eclipse.jetty.server.handler.ContextHandler - scope null||/ @ o.e.j.s.ServletContextHandler{/,null} 
2013-07-23 11:06:16,641 [qtp22355327-17 - /] DEBUG org.eclipse.jetty.server.handler.ContextHandler - context=||/ @ o.e.j.s.ServletContextHandler{/,null} 
2013-07-23 11:06:16,641 [qtp22355327-17 - /] DEBUG org.eclipse.jetty.servlet.ServletHandler - servlet ||/ -> null 
2013-07-23 11:06:16,641 [qtp22355327-17 - /] DEBUG org.eclipse.jetty.servlet.ServletHandler - chain=null 
2013-07-23 11:06:16,641 [qtp22355327-17 - /] DEBUG org.eclipse.jetty.servlet.ServletHandler - Not Found/
2013-07-23 11:06:16,647 [qtp22355327-17 - /] DEBUG org.eclipse.jetty.server.Server - RESPONSE/404 
2013-07-23 11:06:16,651 [qtp22355327-17] DEBUG org.eclipse.jetty.http.HttpParser - filled 0/0 
2013-07-23 11:06:16,769 [qtp22355327-11] DEBUG org.eclipse.jetty.http.HttpParser - filled 279/279 
2013-07-23 11:06:16,770 [qtp22355327-11 - /favicon.ico] DEBUG org.eclipse.jetty.server.Server - REQUEST /favicon.ico on [email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-5,l=14,c=0},r=2 
2013-07-23 11:06:16,770 [qtp22355327-11 - /favicon.ico] DEBUG org.eclipse.jetty.server.handler.ContextHandler - scope null||/favicon.ico @ o.e.j.s.ServletContextHandler{/,null} 
2013-07-23 11:06:16,771 [qtp22355327-11 - /favicon.ico] DEBUG org.eclipse.jetty.server.handler.ContextHandler - context=||/favicon.ico @ o.e.j.s.ServletContextHandler{/,null} 
2013-07-23 11:06:16,771 [qtp22355327-11 - /favicon.ico] DEBUG org.eclipse.jetty.servlet.ServletHandler - servlet ||/favicon.ico -> null 
2013-07-23 11:06:16,771 [qtp22355327-11 - /favicon.ico] DEBUG org.eclipse.jetty.servlet.ServletHandler - chain=null 
2013-07-23 11:06:16,772 [qtp22355327-11 - /favicon.ico] DEBUG org.eclipse.jetty.servlet.ServletHandler - Not Found /favicon.ico 
2013-07-23 11:06:16,774 [qtp22355327-11 - /favicon.ico] DEBUG org.eclipse.jetty.server.Server - RESPONSE /favicon.ico 404 
2013-07-23 11:06:16,775 [qtp22355327-11] DEBUG org.eclipse.jetty.http.HttpParser - filled 0/0 
2013-07-23 11:06:26,564 [qtp22355327-15] DEBUG org.eclipse.jetty.io.nio.ChannelEndPoint - ishut [email protected]{l(/127.0.0.1:61293)<->r(/127.0.0.1:8080),d=true,open=true,ishut=false,oshut=false,rb=false,wb=false,w=true,i=1r}-{[email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=-14,l=0,c=0},r=0} 
2013-07-23 11:06:26,564 [qtp22355327-15] DEBUG org.eclipse.jetty.http.HttpParser - filled -1/0 
2013-07-23 11:06:26,566 [qtp22355327-15] DEBUG org.eclipse.jetty.io.nio.ChannelEndPoint - close [email protected]{l(/127.0.0.1:61293)<->r(/127.0.0.1:8080),d=true,open=true,ishut=true,oshut=false,rb=false,wb=false,w=true,i=1r}-{[email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=0},r=0} 
2013-07-23 11:06:26,568 [qtp22355327-10 Selector0] DEBUG org.eclipse.jetty.io.nio - destroyEndPoint [email protected]{l(/127.0.0.1:61293)<->r(/127.0.0.1:8080),d=true,open=false,ishut=true,oshut=true,rb=false,wb=false,w=true,i=1!}-{[email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=0},r=0} 
2013-07-23 11:06:26,572 [qtp22355327-10 Selector0] DEBUG org.eclipse.jetty.server.AbstractHttpConnection - closed [email protected],g=HttpGenerator{s=0,h=-1,b=-1,c=-1},p=HttpParser{s=0,l=0,c=0},r=0 

回答

2

你需要一個默認的servlet。試着增加你的吉斯過濾後加入

context.addServlet(DefaultServlet.class, "/");