CXF / Jetty相当于以下Jersey / Jetty代码

时间:2015-01-18 14:19:09

标签: java jersey jetty cxf jax-rs

我有一些测试代码与Jersey(2.7)和Jetty(9.2.5.v2014112),我试图移植到CXF(3.0.x)和Jetty(相同版本)。我无法弄清楚其中的一部分,相当于下面的rc.register(resource)行:

    ServletContextHandler sch = new ServletContextHandler();
    sch.setContextPath("/xxx");

    resource = new TheResource();
    ResourceConfig rc = new ResourceConfig();
    rc.register(resource);

    ServletContainer sc = new ServletContainer(rc);        
    ServletHolder holder = new ServletHolder(sc);
    sch.addServlet(holder, "/*");

    Server server = new Server(port);
    server.setHandler(sch);
    server.start();
    server.join();

对于CXF / Jetty案例,我知道我需要做类似的事情:

CXFNonSpringJaxrsServlet context = new CXFNonSpringJaxrsServlet();
// EQUIVALENT OF:
//     ResourceConfig rc = new ResourceConfig();
//     rc.register(myresource)
//     SOMETHING.setResourceConfig(rc)
ServletHolder holder = new ServletHolder(context);
ServletContextHandler handler = new ServletContextHandler("/restroot");
handler.addServlet(holder, "/*");

// Using ContextHandlerCollection cos my jetty needs to serve more
// than just this REST service
ContextHandlerCollection collection = new ContextHandlerCollection();
collection.addHandler(handler);

Server server = new Server(8080);
server.setHandler(collection);
server.start();
server.join();

有人知道这是怎么回事?

只是为了澄清我在这里尝试做的事情:我试图在嵌入式Jetty实例上发布JAX-RS服务,我需要完全控制 JAX-RS bean的生命周期。

马腾

3 个答案:

答案 0 :(得分:0)

查看CXFNonSpringJaxrsServlet的源代码,它是通过web.xml中的init params配置的。但是,你可以自由地覆盖当然的功能

答案 1 :(得分:0)

我设法得到了一些工作:

  • 延长CXFNonSpringJaxrsServlet
  • 覆盖getResourceProviders()
  • 提供我自己的ResourceProvider
  • 实施方案

这看起来有点复杂,我觉得在CXF中应该有一个标准的API,但无论如何,这是我的解决方案:

public class TheApp {
    private static final Logger LOG = LoggerFactory.getLogger(TheApp.class);

    TheResource resource;

    public static class BeanResourceProvider implements ResourceProvider {
        private final Object theBean;

        public BeanResourceProvider(Object theBean) {
            this.theBean = theBean;
        }

        @Override
        public Object getInstance(Message m) {
            return theBean;
        }

        @Override
        public void releaseInstance(Message m, Object o) {            
        }

        @Override
        public Class<?> getResourceClass() {
            return theBean.getClass();
        }

        @Override
        public boolean isSingleton() {
            return true;
        }

    };

    public TheApp(int port) throws Exception {
        resource = new TheResource();

        CXFNonSpringJaxrsServlet context = new CXFNonSpringJaxrsServlet() {
            public void configureSingleton(Object o) {
                LOG.info("configureSingleton() called");
            }

            public Map<Class<?>, ResourceProvider>
                    getResourceProviders(ServletConfig servletConfig,
                            Map<Class<?>, Map<String, List<String>>> resourceClasses) throws ServletException {
                LOG.info("getResourceProviders called");

                Map<Class<?>, ResourceProvider> map = new HashMap<Class<?>, ResourceProvider>();
                for (Map.Entry<Class<?>, Map<String, List<String>>> entry : resourceClasses.entrySet()) {
                    Class<?> c = entry.getKey();
                    LOG.info("getting provider for {}", c.getName());
                    map.put(c, new BeanResourceProvider(resource));
                }
                return map;
            }
        };

        ServletHolder holder = new ServletHolder(context);
        holder.setInitParameter("jaxrs.serviceClasses", "com.test.cxfjetty.TheResource");

        ServletContextHandler handler = new ServletContextHandler();
        handler.addServlet(holder, "/*");
        handler.setContextPath("/theroot");

        Server server = new Server(port);
        server.setHandler(handler);
        server.start();
        server.join();
    }

    public static void main(String[] args) throws Exception {
        TheApp app = new TheApp(8122);        
    }
}
这是有趣的运动......

答案 2 :(得分:0)

从CXF 3.0.4版开始,CXFNonSpringJaxrsServlet上还有其他构造函数可以使这更容易:

public SNAPSHOTApp(int port) throws Exception {
    resource = new TheResource();
    Set<Object> resourceSingletons = new HashSet<>();
    resourceSingletons.add(resource);

    CXFNonSpringJaxrsServlet context = new CXFNonSpringJaxrsServlet(resourceSingletons);

    ServletHolder servlet = new ServletHolder(context);
    ServletContextHandler handler = new ServletContextHandler();
    handler.addServlet(servlet, "/*");
    handler.setContextPath("/snapshot");

    Server server = new Server(port);
    server.setHandler(handler);
    server.start();
    server.join();
}

非常感谢@Sergey Beryozkin根据我在CXF用户邮件列表上的问题添加此内容。