2011-10-07 32 views
1

我使用Google Guice和Jersey(jax-rs)。以下方法拋出的JAXB異常(JAXB不能處理接口),如果我稱之爲:Google Guice和Jersey - JAXB無法處理接口錯誤

@POST 
public void addUser(UserTO user){ 
} 

用戶至一個接口,但在吉斯我把它綁定到實現:

bind(UserTO.class).to(DefaultUserTO.class); 

我認爲Guice應該能夠處理這個問題。但也許東西在我的服務器啓動是錯誤的:

Injector injector = 
     Guice.createInjector(new GuiceServerModule(), 
          new JerseyServletModule() { 
       @Override 
       protected void configureServlets() { 
        // Route all requests through GuiceContainer 
        serve("/*").with(GuiceContainer.class); 
       } 
     }); 

    // Create the server. 
    Server server = new Server(12345); 

    // Create a servlet context and add the jersey servlet. 
    ServletContextHandler sch = new ServletContextHandler(server, "/"); 

    // Add our Guice listener that includes our bindings 
    sch.addEventListener(new GuiceServletConfig(injector)); 

    // Then add GuiceFilter and configure the server to 
    // reroute all requests through this filter. 
    sch.addFilter(GuiceFilter.class, "/*", null); 

    // Must add DefaultServlet for embedded Jetty. 
    // Failing to do this will cause 404 errors. 
    // This is not needed if web.xml is used instead. 
    sch.addServlet(DefaultServlet.class, "/"); 

    // Start the server 
    server.start(); 

    // Wait until server shut down 
    server.join(); 

還是我只使用一個實現?

回答

2

您需要使用具體的類。吉斯不在這裏。用戶實例由Jersey使用JAXB創建。沒有任何Guice(或任何其他DI框架)可以做。您還應該刪除UserTO的綁定。一般來說,我認爲使用DI框架管理表示數據的對象並不是一個好主意。