How to get ClientResponseFailure information in RestEasy Client?

How to get the contents of an HTTP response when stats> = 400 is returned. This is my sample code:

    try {
        ChatService client = ProxyFactory.create(ChatService.class, apiUrl);
        client.putMessage(dto);
    } catch (ClientResponseFailure ex) {
        System.out.println(ex.getResponse().getEntity().toString());
    }

It 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 would like to have more details than just the 400 status code.

+3
source share
2 answers

Is this the exception you wanted to send?

Unfortunately, the RestEASY client environment does not support exception marking by itself and instead adapts it to the HTTP infrastructure. Exceptions should still be thrown to the server. I never did this, you can use ExceptionMappers for marked exceptions.

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

0

, streamFactory XML. RestEasy ClientResponse. :

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

getEntity . , , ServiceError. , , 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

Source: https://habr.com/ru/post/1751393/


All Articles