使用带有嵌入式码头和物体注入的Atmosphere-jersey

时间:2013-10-30 15:12:04

标签: java rest jersey atmosphere

我有一个在嵌入式码头上运行的球衣应用程序。我想把它转换为使用大气平纹球来利用彗星功能,我有点卡住了。任何帮助非常感谢。 我的球衣代码使用配置了ResourceConfig的球衣ServletContainer来指定资源实例

ResourceConfig.registerInstances(resourceInstance);
new org.glassfish.jersey.servlet.ServletContainer(ResourceConfig);

我无法弄清楚如何使用相同的设置来处理氛围。我可以看到有一个AtmosphereServlet。是否可以为其提供资源实例?

这就是现在如何设置球衣/码头...

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.glassfish.jersey.server.ResourceConfig;

public class HelloWorldJerseyJetty {

    // http://localhost:8080/resources/resource

    public static void main(String[] args) throws Exception {

        Server server = new Server(8080);

        ResourceConfig config = new ResourceConfig();
        config.registerInstances(new Resource("Hello World"));
        org.glassfish.jersey.servlet.ServletContainer jerseyServlet = new org.glassfish.jersey.servlet.ServletContainer(config);

        ServletContextHandler handler = new ServletContextHandler();
        handler.addServlet(new ServletHolder(jerseyServlet), "/resources/*");

        server.setHandler(handler);
        server.start();
        server.join();

    }

    @Path("resource")
    public static class Resource {

        private final String message;

        public Resource(String message) {
            this.message = message;
        }

        @GET
        @Produces(MediaType.TEXT_PLAIN)
        public String get() {
            return message;
        }
    }
}

如何从球衣切换到AtmosphereServlet以及如何配置它以使用实例?

        ResourceConfig config = new ResourceConfig();
        config.registerInstances(new Resource("Hello World"));
        AtmosphereServlet atmosphereServlet = new AtmosphereServlet();
        atmosphereServlet.???

        ServletContextHandler handler = new ServletContextHandler();
        handler.addServlet(new ServletHolder(atmosphereServlet), "/resources/*");

1 个答案:

答案 0 :(得分:0)

我像这样配置AtmosphereServlet并且它可以工作。 也许你的问题可能为时已晚,但也可能对其他人有所帮助:)

    Server server = new Server(8080); 

    //static files handler        
    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(true);
    resource_handler.setWelcomeFiles(new String[] { "index.html" });
    resource_handler.setResourceBase("./src/main/webapp/");

    ContextHandler context1 = new ContextHandler();
    context1.setHandler(resource_handler);

    //Atmosphere
    AtmosphereServlet atmosphereServlet = new AtmosphereServlet();
    ServletHolder servletHolder = new ServletHolder(atmosphereServlet);
    servletHolder.setInitParameter("com.sun.jersey.config.property.packages","org.atmosphere.samples.chat.jersey");
    servletHolder.setInitParameter("org.atmosphere.websocket.messageContentType", "application/json");
    servletHolder.setAsyncSupported(true);
    servletHolder.setInitParameter("org.atmosphere.useWebSocket","true");

    ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
    context.addServlet(servletHolder, "/resources/*");

    HandlerList handlers = new HandlerList();
    handlers.addHandler(context1);
    handlers.addHandler(context);        

    server.setHandler(handlers);