StreamingOutput没有抛出WebApplicationException,返回空响应

时间:2014-05-08 20:20:16

标签: java jersey-2.0

我在WebApplicationException实施中投出StreamingOutput时遇到了困难。我希望下面的代码返回501,但curl报告curl: (52) Empty reply from server。我可以在通话的痕迹中看到503,但是泽西只是回复了一个空洞的身体。有人知道是什么给出的吗?

public final class MyStreamingOutput implements StreamingOutput {

    @Override
    public void write(final OutputStream outputStream)
        throws IOException {

        try {
            dataProvider = new DataProvider(); // throws SQLException
        } catch (final SQLException e) {
            throw new WebApplicationException(503);
        }
    }
}

这是我看到的痕迹:

java.sql.SQLException: Invalid object name 'no_such_table'.
    at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368) ~[jtds-1.2.4.jar:1.2.4]
...
16:05:22.148 [http-8080-1] DEBUG org.glassfish.jersey.tracing.general - MBW_WRITE_TO WriteTo by [org.glassfish.jersey.message.internal.StreamingOutputProvider @1d45d135] [642.98 ms]
16:05:22.148 [http-8080-1] DEBUG org.glassfish.jersey.tracing.general - WI_AFTER [org.glassfish.jersey.filter.LoggingFilter @77edc290 #-2147483648] AFTER context.proceed() [ 0.00 ms]
16:05:22.148 [http-8080-1] DEBUG org.glassfish.jersey.tracing.general - WI_AFTER [org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor @2a0fded2 #3000] AFTER context.proceed() [ 0.01 ms]
16:05:22.148 [http-8080-1] DEBUG org.glassfish.jersey.tracing.general - WI_AFTER [org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor @26c087be #10] AFTER context.proceed() [ 0.01 ms]
16:05:22.148 [http-8080-1] DEBUG org.glassfish.jersey.tracing.general - WI_SUMMARY WriteTo summary: 3 interceptors [644.44 ms]
16:05:22.148 [http-8080-1] DEBUG org.glassfish.jersey.tracing.general - FINISHED Response status: 200/SUCCESSFUL|OK [ ---- ms]
16:05:22.153 [http-8080-1] DEBUG org.glassfish.jersey.tracing.general - EXCEPTION_MAPPING Exception mapper [com.locustec.eim.query.rest.RuntimeExceptionMapper @3a8978c7] maps [javax.ws.rs.WebApplicationException @563625d0 <501/SERVER_ERROR|Not Implemented|-no-entity->] ('Carp') to <501/SERVER_ERROR|Not Implemented> [ 0.02 ms]
16:05:22.153 [http-8080-1] INFO  o.g.jersey.filter.LoggingFilter - 8 * LoggingFilter - Response received on thread http-8080-1
8 < 503

16:05:22.154 [http-8080-1] DEBUG org.glassfish.jersey.tracing.general - RESPONSE_FILTER Filter by [org.glassfish.jersey.filter.LoggingFilter @5271b383 #-2147483648] [ 0.13 ms]
16:05:22.154 [http-8080-1] DEBUG org.glassfish.jersey.tracing.general - RESPONSE_FILTER_SUMMARY Response summary: 1 filters [ 0.93 ms]
16:05:22.155 [http-8080-1] DEBUG org.glassfish.jersey.tracing.general - FINISHED Response status: 501/SERVER_ERROR|Not Implemented [ ---- ms]
16:05:22.160 [http-8080-1] TRACE o.g.j.process.internal.RequestScope - [DEBUG] Released scope instance Instance{id=021c24a2-c224-4c22-8f18-e5f7f93b0295, referenceCounter=0, store size=0} on thread http-8080-1

1 个答案:

答案 0 :(得分:1)

Jersey正在开始HTTP响应,因此发送200 OK,然后才开始调用StreamingOutput.write实现。因此,当引发异常时,发送不同的HTTP代码已经太晚了。

我发现处理此类问题的最佳方法是尽可能地在StreamingOutput之外或在实现的构造函数中执行(这些版本将发送500,您可以捕获SQLException如果您还想要其他内容):

@GET
public StreamingOutput getDataWithAnonymousClass() throws SQLException {
    final DataProvider dataProvider = new DataProvider();
    return new StreamingOutput() {
        @Override
        public void write(OutputStream output) {
            dataProvider.writeData(output);
        }
    }
}


public final class MyStreamingOutput implements StreamingOutput {

    private final DataProvider dataProvider;

    public MyStreamingOutput() throws SQLException {
        this.dataProvider = new DataProvider();
    }

    @Override
    public void write(OutputStream output) {
        dataProvider.writeData(output);
    }
}

@GET
public StreamingOutput getDataWithExcInConstructor() throws SQLException {
    return new MyStreamingOutput();
}

这样,在HTTP响应启动之前抛出异常,您可以拥有不同的状态。