返回状态> = 400时如何获取http响应内容。这是我的代码示例:
try {
ChatService client = ProxyFactory.create(ChatService.class, apiUrl);
client.putMessage(dto);
} catch (ClientResponseFailure ex) {
System.out.println(ex.getResponse().getEntity().toString());
}
这引发:
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
我想了解更多详情,而不仅仅是状态代码400.
答案 0 :(得分:0)
这是您要发送的例外吗?
不幸的是,RestEASY客户端框架本身不支持异常编组,而是将其调整到HTTP框架中。但是仍然应该在服务器上抛出异常。我从来没有这样做过,你可以使用ExceptionMappers来检查异常。
http://docs.jboss.org/resteasy/docs/1.2.GA/userguide/html/ExceptionHandling.html
答案 1 :(得分: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());
}