使用web.xml在OSGi Jetty上进行REST实现

时间:2019-05-16 05:19:17

标签: java rest jetty osgi

使用web.xml在OSGi Jetty上尝试简单的REST实现

以下存储库包含我尝试过的代码: 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脚本。

要运行代码,请在traget文件夹中git close和mvn clean install ..运行run.sh即可运行该脚本。

我试图寻找与配置有关的问题。我无法准确解决此问题的错误消息。

下面是我的实现细节:

资源文件:

@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>JAXRSEx</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>JAXRSEx</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>

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

进行了部署

它与简单激活器一起使用:

@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);
}

我希望正确部署应用程序jar。

以下是我得到的错误:

05:59:46.779 [Start Level: Equinox Container: 82edcca2-ba11-4354-8c49-96dd914910fc] WARN  o.eclipse.jetty.webapp.WebAppContext - Failed startup of context o.e.j.w.WebAppContext@70993f30{/controller/nb/v2/monitor,file:///tmp/jetty-0.0.0.0-8081-my-learning-osgi-simple-rest-with-jetty-1.0-SNAPSHOT.jar-_controller_nb_v2_monitor-any-1013570667046445682.dir/webapp/,UNAVAILABLE}{file:/home/ndb/dev/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}
java.lang.IllegalArgumentException: null
    at org.objectweb.asm.ClassVisitor.<init>(Unknown Source)
    at org.objectweb.asm.ClassVisitor.<init>(Unknown Source)
    at org.eclipse.jetty.annotations.AnnotationParser$MyClassVisitor.<init>(AnnotationParser.java:476)
    at org.eclipse.jetty.annotations.AnnotationParser.scanClass(AnnotationParser.java:935)
    at org.eclipse.jetty.osgi.annotations.AnnotationParser.parse(AnnotationParser.java:217)
    at org.eclipse.jetty.osgi.annotations.AnnotationConfiguration$BundleParserTask.call(AnnotationConfiguration.java:66)
    at org.eclipse.jetty.annotations.AnnotationConfiguration$1.run(AnnotationConfiguration.java:471)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:765)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:683)
    at java.lang.Thread.run(Thread.java:748)
____________________________
Welcome to Apache Felix Gogo

g! 

0 个答案:

没有答案