2014-05-08 22 views
1

我很難從我的StreamingOutput實現中拋出WebApplicationException。我期望下面的代碼返回一個501,但是curl報告爲curl: (52) Empty reply from server。我可以看到電話中的503號,但澤西島只是回覆空的身體。有人知道給了什麼嗎?StreamingOutput不拋出WebApplicationException,返回空的響應

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 
+0

501/503不匹配是否爲錯字?我遇到同樣的問題,試圖將運行時異常從第三方庫映射到合理的狀態碼 - 還沒有運氣:( – Sonata

+0

是的,這是一個錯字。任何一個都可以。只是不是500,或者我所看到的,沒有什麼 - 完全空洞的迴應。 –

回答

1

新澤西開始的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響應啓動之前拋出異常,並且可以有不同的狀態。