我在从WebApplicationException实现中抛出StreamingOutput时遇到困难。我希望下面的代码返回501,但是curl报告curl: (52) Empty reply from server。在通话记录中可以看到503,但Jersey只是在空着的身躯下回覆。有人知道会给什么吗?

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

最佳答案

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响应之前会引发异常,并且您可以具有其他状态。

关于java - StreamingOutput不引发WebApplicationException,返回空响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23551741/

10-11 03:34