OSGi Equinox + Jetty-使用web.xml的简单REST部署

时间:2019-01-13 07:16:17

标签: java rest jetty osgi equinox

在OSGi Equinox + Jetty上尝试简单的REST实现-使用web.xml进行简单的REST部署

下面是我的实现细节:

资源文件:

@Path("jsonstatus")
public class JsonResource {

    private static Logger logger = LoggerFactory.getLogger(JsonResource.class);

    @GET
    @Produces("application/json")
    public String listConferences() {
        logger.info("Returning the status");
        return "{\"status\": \"success\"}";
    }
}

应用实现:

public class JerseyApplication extends Application {

    private static Logger logger = LoggerFactory.getLogger(JerseyApplication.class.getName());
    @Override
    public Set<Class<?>> getClasses() {
        logger.info("--Returning all the claess-");
        Set<Class<?>> result = new HashSet<Class<?>>();
        result.add(JsonResource.class);
        result.add(StatusResource.class);
        return result;
    }
}

web.xml:

  <servlet>
    <servlet-name>SimpleREST</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>my.learnings.osgi.rest.jetty.activator.JerseyApplication</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>SimpleREST</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

所有OSGi核心,控制台和码头罐子都添加了:启动Equinox时,出现以下错误。当我使用web.xml部署时,我没有将激活器映射到捆绑软件中

但是删除了激活器和web.xml之后,REST API可以正常工作,在春分启动过程中没有错误。

简单激活器:

@Override
public void start(BundleContext context) throws Exception {
    logger.info("Starting the bundle");
    ServletContextHandler ch = new ServletContextHandler();
    ch.setContextPath("/");
    ServletHolder holder = new ServletHolder(new ServletContainer());
    holder.setInitParameter("javax.ws.rs.Application", JerseyApplication.class.getName());
    ch.addServlet(holder, "/*");
    context.registerService(ContextHandler.class.getName(), ch, null);
}

在Internet上搜索时,我发现需要配置JNDI,这是非常基本的简单REST,不需要数据库。因此感到困惑,为什么我们需要JNDI。一些文章讨论了有关java.naming.factory.initial的初始化。但是那些是给jboss的。不确定如何为码头配置初始上下文。我尝试过的所有组合都在github代码中。

如果可以帮助您在OSGi-jetty中配置初始上下文,那将真的有很大的帮助。我正在使用码头在OSGi上以web.xml方式创建servlet的部署方式

以下存储库包含我尝试过的代码: https://github.com/pkolanda/my-learning-osgi-simple-rest-with-jetty  要运行代码,请执行mvn clean install并在target / my-learning-osgi-simple-rest-with-jetty-0.1文件夹下运行run.sh shell脚本。

与此同时,我正在调查这篇文章 日志:

11:14:33.951 [Start Level: Equinox Container: 6ee1939d-123f-4def-860f-dfc2e21bdfc4] WARN  o.eclipse.jetty.webapp.WebAppContext - Failed startup of context o.e.j.w.WebAppContext@7c19344a{/my-learning-osgi-simple-rest-with-jetty-1.0-SNAPSHOT,file:///tmp/jetty-0.0.0.0-8081-my-learning-osgi-simple-rest-with-jetty-1.0-SNAPSHOT.jar-_my-learning-osgi-simple-rest-with-jetty-1.0-SNAPSHOT-any-9046322855444487913.dir/webapp/,UNAVAILABLE}{file:/home/prakash/mygit/my-learning-osgi-simple-rest-with-jetty/target/my-learning-osgi-simple-rest-with-jetty-0.1/plugins/my-learning-osgi-simple-rest-with-jetty-1.0-SNAPSHOT.jar}
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:313)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:350)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at org.eclipse.jetty.plus.webapp.EnvConfiguration.createEnvContext(EnvConfiguration.java:258)
    at org.eclipse.jetty.plus.webapp.EnvConfiguration.preConfigure(EnvConfiguration.java:67)
    at org.eclipse.jetty.webapp.WebAppContext.preConfigure(WebAppContext.java:506)
    at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:544)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.deploy.bindings.StandardStarter.processBinding(StandardStarter.java:46)
    at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:192)
    at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:505)
    at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:151)
    at org.eclipse.jetty.osgi.boot.BundleWebAppProvider.bundleAdded(BundleWebAppProvider.java:214)
    at org.eclipse.jetty.osgi.boot.BundleWebAppProvider$WebAppTracker.addingBundle(BundleWebAppProvider.java:80)
    at org.osgi.util.tracker.BundleTracker$Tracked.customizerAdding(BundleTracker.java:475)
    at org.osgi.util.tracker.BundleTracker$Tracked.customizerAdding(BundleTracker.java:1)
    at org.osgi.util.tracker.AbstractTracked.trackAdding(AbstractTracked.java:256)
    at org.osgi.util.tracker.AbstractTracked.track(AbstractTracked.java:229)
    at org.osgi.util.tracker.BundleTracker$Tracked.bundleChanged(BundleTracker.java:450)
    at org.eclipse.osgi.internal.framework.BundleContextImpl.dispatchEvent(BundleContextImpl.java:911)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:233)
    at org.eclipse.osgi.framework.eventmgr.ListenerQueue.dispatchEventSynchronous(ListenerQueue.java:151)
    at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEventPrivileged(EquinoxEventPublisher.java:233)
    at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEvent(EquinoxEventPublisher.java:140)
    at org.eclipse.osgi.internal.framework.EquinoxEventPublisher.publishBundleEvent(EquinoxEventPublisher.java:132)
    at org.eclipse.osgi.internal.framework.EquinoxContainerAdaptor.publishModuleEvent(EquinoxContainerAdaptor.java:194)
    at org.eclipse.osgi.container.Module.publishEvent(Module.java:483)
    at org.eclipse.osgi.container.Module.start(Module.java:474)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1783)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.incStartLevel(ModuleContainer.java:1763)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.doContainerStartLevel(ModuleContainer.java:1725)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1656)
    at org.eclipse.osgi.container.ModuleContainer$ContainerStartLevel.dispatchEvent(ModuleContainer.java:1)
    at org.eclipse.osgi.framework.eventmgr.EventManager.dispatchEvent(EventManager.java:233)
    at org.eclipse.osgi.framework.eventmgr.EventManager$EventThread.run(EventManager.java:343)
____________________________
Welcome to Apache Felix Gogo

g! 

0 个答案:

没有答案