Google Guice 和 Jersey - JAXB 无法处理接口错误

发布于 2024-12-08 17:11:27 字数 1493 浏览 0 评论 0原文

我正在使用 Google Guice 和 Jersey (jax-rs)。如果我调用以下方法,它会抛出 JAXB 异常(JAXB 无法处理接口):

@POST
public void addUser(UserTO user){
}

UserTO 是一个接口,但在 Guice 中我将它绑定到一个实现:

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

或者我必须只使用一个实现吗?

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
                  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妳是的陽光 2024-12-15 17:11:27

您需要使用具体的类。吉斯并不参与其中。用户实例由 Jersey 使用 JAXB 创建。 Guice(或任何其他 DI 框架)无能为力。您还应该删除 UserTO 的绑定。一般来说,我认为让 DI 框架管理表示数据的对象并不是一个好主意。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文