开发者

Google Guice and Jersey - JAXB can't handle interfaces Error

开发者 https://www.devze.com 2023-04-12 00:28 出处:网络
I\'m using Google Guice with Jersey (jax-rs). Following method throws an JAXB-Exception (JAXB can\'t handle interfaces) if I calling it:

I'm using Google Guice with Jersey (jax-rs). Following method throws an JAXB-Exception (JAXB can't handle interfaces) if I calling it:

@POST
public void addUser(UserTO user){
}

UserTO is an interface, but in Guice I bound it to an implementation:

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

I thought Guice should be able to handle this. But maybe something in my server startup is wrong:

    Injector injector =
      Guice.createInjector(new GuiceServerModule(),
                           new JerseyServletModule() {
              @Override
              protected void configureServlets() { 
                  // Route all requests through GuiceContainer
    开发者_如何学Python              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();

Or do I have to use only an implementation?


You need to use a concrete class. Guice is not in the game here. The user instance is created by Jersey using JAXB. There is nothing Guice (or any other DI framework) could do. You should also remove the binding for UserTO. Generally, I'd say it is not a good idea to have a DI framework manage objects representing data.

0

精彩评论

暂无评论...
验证码 换一张
取 消