Google Guice 和 Jersey - JAXB 无法处理接口错误
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用具体的类。吉斯并不参与其中。用户实例由 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.