使用带有Tomcat 7的JAX-RS Jersey 2.3.1无法使ChunkedOutput正常工作

时间:2013-10-09 03:09:59

标签: java eclipse rest tomcat jax-rs

我无法使用JAX-RS Jersey 2.3.1与Tomcat 7一起使用ChunkedOutput。我正在尝试按照Jersey Tutorial - Chapter 10. Asynchronous Services and Clients

中的教程进行操作

我的代码如下所示

@GET
@Path("/asynchronous")
public ChunkedOutput<String> getAsyncResponse() {

    final ChunkedOutput<String> output = new ChunkedOutput<String>(String.class);

    new Thread() {
        public void run() {
            try {
                String text;
                for (int i = 0; i < 20; i++) {
                    text = String.valueOf(i);
                    output.write(text);
                    Thread.sleep(1000);
                }
            } catch (Throwable th) {
                th.printStackTrace();
            } finally {
                try {
                    output.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }.start();

    return output;
}

当我尝试通过curl或我的Java客户端调用它时,我在Web服务端得到以下异常。

org.glassfish.jersey.server.internal.process.MappableException: java.lang.NullPointerException
    at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:96)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:149)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1139)
    at org.glassfish.jersey.server.ChunkedOutput$1.call(ChunkedOutput.java:148)
    at org.glassfish.jersey.server.ChunkedOutput$1.call(ChunkedOutput.java:121)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:242)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:345)
    at org.glassfish.jersey.server.ChunkedOutput.flushQueue(ChunkedOutput.java:121)
    at org.glassfish.jersey.server.ChunkedOutput.write(ChunkedOutput.java:111)
    at jaxrs.prototype.tomcat7.AsyncPrototype$1.run(AsyncPrototype.java:82)
Caused by: java.lang.NullPointerException
    at org.apache.coyote.http11.InternalOutputBuffer.realWriteBytes(InternalOutputBuffer.java:215)
    at org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:480)
...

知道出了什么问题吗?最初我在Tomcat 6上部署它然后我意识到Servlet 3.0 API中提供了异步功能,而Tomcat 6不支持,因此我切换到Tomcat 7。

1 个答案:

答案 0 :(得分:0)

在web.xml中添加async-supported对我来说是个窍门。之后我再也看不到异常了。

虽然我的客户端仍有问题,但每次写入时都没有收到分块输出。相反,它最终会同时接收所有分块输出。

  <servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>jaxrs.prototype;org.codehaus.jackson.jaxrs</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>