在JDK 1.6上使用嵌入式Jetty的Jersey ChunkedOutput

时间:2014-03-05 21:09:44

标签: jersey jetty jdk1.6

我正在尝试使用org.glassfish.jersey.server.ChunkedOutput ...扩展示例here以包含服务...

package com.example;
...

@Path("/myresource")
public class MyResource {

  @GET
  public ChunkedOutput<String> foo() {
    final ChunkedOutput<String> output = new ChunkedOutput<String>(
        String.class);

    new Thread() {
      public void run() {
        try {
          output.write("Hello world");
        } catch (Exception ex) {
          //Empty
        } finally {
          try {
            output.close();
          } catch (IOException e) {
            e.printStackTrace();
          }
        }
      }
    }.start();

    return output;
  }
}

我添加了以下依赖项...

<dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-jetty-http</artifactId>
  <version>2.5</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.core</groupId>
  <artifactId>jersey-server</artifactId>
  <version>2.5</version>
</dependency>
<dependency>
  <groupId>org.glassfish.jersey.containers</groupId>
  <artifactId>jersey-container-servlet-core</artifactId>
  <version>2.5</version>
</dependency>

Jetty设置的主要内容如下所示......

public class EmbedMe {
  public static void main(String[] args) throws Exception {
    int port = 8080;
    Server server = new Server(port);

    String wardir = "target/sample-webapp-1-SNAPSHOT";

    WebAppContext context = new WebAppContext();
    context.setResourceBase(wardir);
    context.setDescriptor(wardir + "WEB-INF/web.xml");
    context.setConfigurations(new Configuration[] {
        new AnnotationConfiguration(), new WebXmlConfiguration(),
        new WebInfConfiguration(), new TagLibConfiguration(),
        new PlusConfiguration(), new MetaInfConfiguration(),
        new FragmentConfiguration(), new EnvConfiguration() });

    context.setContextPath("/");
    context.setParentLoaderPriority(true);
    server.setHandler(context);

    // added for Jersey
    ServletHolder servletHolder = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/webapi/*");
    servletHolder.setAsyncSupported(true);
    servletHolder.setInitParameter("jersey.config.server.provider.packages","com.example");

    server.start();
    server.dump(System.err);
    server.join();
  }
}

当我跑步时,我看到了......

java.lang.UnsupportedClassVersionError: org/eclipse/jetty/server/Handler : Unsupported major.minor version 51.0

我认为那是因为我使用的是jdk 1.6而jersey-container-jetty-http取决于jetty版本9.0.6.v20130930(jdk 1.7)。嵌入式Jetty / servlet 3示例的原始代码使用jetty版本8.1.14.v20131031和jdk 1.6。

所以3个问题

  1. jersey-container-jetty-http是否是ChunkedOutput的正确依赖?
  2. 是否可以将ChunkedOutput与嵌入式Jetty和JDK 1.6一起使用?
  3. 如果有可能,有人有一个有效的例子吗?

0 个答案:

没有答案