开发者

How to get details of ClientResponseFailure in RestEasy Client?

How to get http response contents when status >=400 is returned. That's my code sample :

    try {
        ChatService client = ProxyFactory.create(ChatService.class, apiUrl);
        client.putMessage(dto);
    } catch (ClientResponseFailure ex) {
        System.out.println(ex.get开发者_JS百科Response().getEntity().toString());
    }

This throws :

Exception in thread "main" org.jboss.resteasy.spi.ReaderException: java.io.IOException: Stream closed
    at org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:123)
    at org.jboss.resteasy.client.core.BaseClientResponse.readFrom(BaseClientResponse.java:246)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:210)
    at org.jboss.resteasy.client.core.BaseClientResponse.getEntity(BaseClientResponse.java:171)
    at App.main(App.java:40)
Caused by: java.io.IOException: Stream closed
    at java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:134)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
    at org.jboss.resteasy.client.core.SelfExpandingBufferredInputStream.read(SelfExpandingBufferredInputStream.java:58)
    at java.io.FilterInputStream.read(FilterInputStream.java:90)
    at org.jboss.resteasy.client.core.SelfExpandingBufferredInputStream.read(SelfExpandingBufferredInputStream.java:68)
    at org.jboss.resteasy.util.ReadFromStream.readFromStream(ReadFromStream.java:30)
    at org.jboss.resteasy.plugins.providers.ByteArrayProvider.readFrom(ByteArrayProvider.java:32)
    at org.jboss.resteasy.plugins.providers.ByteArrayProvider.readFrom(ByteArrayProvider.java:23)
    at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:105)
    at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:46)
    at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:108)
    at org.jboss.resteasy.core.messagebody.ReaderUtility.doRead(ReaderUtility.java:111)
    ... 4 more

I'd like to have more details than just status code 400.


Is that the exception you meant to send?

Unfortunately the RestEASY client framework doesn't support Exception marshalling per se, and instead adapts it into the HTTP framework. Exceptions should still be thrown on the server though. I've never done it, you can use ExceptionMappers for checked Exceptions.

http://docs.jboss.org/resteasy/docs/1.2.GA/userguide/html/ExceptionHandling.html


When debugging I noticed that the details I needed were in the 'streamFactory' object as a byte stream of XML. I found this help topic in the RestEasy docs about ClientResponse. It says

getEntity(java.lang.Class<T2> type) 

where getEntity can marshal the output to the desired class. In my case, I have a custom class for errors returned from services called ServiceError. So, that was the class I passed to getEntity:

try {
    serviceResult = proxy.addCustomer(customerName, customerProfile);
} catch (ClientResponseFailure ex) {
    ClientResponse<String> cResp = ex.getResponse();
    ServiceError myEntity = cResp.getEntity(ServiceError.class);
    System.out.println("myEntity errorText=" + myEntity.getErrorMessage().getErrorText());
    System.out.println("myEntity errorCode=" + myEntity.getErrorMessage().getErrorCode());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜