gpt4 book ai didi

java - StreamingOutput 不抛出 WebApplicationException,返回空响应

转载 作者:太空宇宙 更新时间:2023-11-04 06:49:21 25 4
gpt4 key购买 nike

我在从 StreamingOutput 实现中抛出 WebApplicationException 时遇到困难。我希望下面的代码返回 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com