Java 异常情况下的日志响应正文

Java 异常情况下的日志响应正文,java,gson,retrofit2,Java,Gson,Retrofit2,我正在使用generation进行http调用,并将gson用作转换器。 在某些情况下,当gson尝试将响应转换为对象时,我会抛出异常,我想知道在这种情况下的实际响应是什么。 例如: 这是我收到的异常消息: Expected a string but was BEGIN_OBJECT at line 1 column 26 path $[0].date 执行调用的代码如下所示: Gson gson = gsonBuilder.create(); Retrofit retrofit = (new

我正在使用
generation
进行http调用,并将
gson
用作转换器。 在某些情况下,当gson尝试将响应转换为对象时,我会抛出异常,我想知道在这种情况下的实际响应是什么。
例如: 这是我收到的异常消息:

Expected a string but was BEGIN_OBJECT at line 1 column 26 path $[0].date
执行调用的代码如下所示:

Gson gson = gsonBuilder.create();
Retrofit retrofit = (new retrofit2.Retrofit.Builder()).baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create(gson)).client(httpClient).build();
MyService service = retrofit.create(clazz);
...
Response<T> response = service.call().execute();
Gson-Gson=gsonBuilder.create();
改型改型=(新改型2.reformation.Builder()).baseUrl(baseUrl).addConverterFactory(GsonConverterFactory.create(gson)).client(httpClient.build();
MyService service=reformation.create(clazz);
...
响应=service.call().execute();

当这段代码抛出异常时,我希望以某种方式记录原始响应主体。我该怎么做呢?

我认为这不容易做到。改型似乎没有提供一种跟踪输入流的简单方法(我想到的最自然的地方是
CallAdapter.Factory
,但它不允许无效响应跟踪)

基本上,应该在特定转换器中检测非法响应转换,该转换器的唯一责任是记录无效有效负载。听起来很像装饰设计模式。由于Java(不像Kotlin?)不支持作为一等公民的装饰程序,转发实现可以类似于Google Guava
转发***
类:

ForwardingInputStream.java
@SuppressWarnings(“资源”)
抽象类ForwardingInputStream
扩展输入流{
受保护的抽象InputStream InputStream();
//@formatter:off
@Override public int read()抛出IOException{return inputStream().read();}
//@formatter:on
//@formatter:off
@重写公共int-read(最终字节[]b)引发IOException{return inputStream().read(b);}
@重写公共int read(最终字节[]b,最终int off,最终int len)引发IOException{return inputStream().read(b,off,len);}
@重写公共长跳过(最后一个长n)抛出IOException{return inputStream().skip(n);}
@Override public int available()抛出IOException{返回inputStream().available();}
@重写公共void close()引发IOException{inputStream().close();}
@重写公共无效标记(最终int readlimit){inputStream().mark(readlimit);}
@重写公共void reset()引发IOException{inputStream().reset();}
@重写公共布尔值markSupported(){return inputStream().markSupported();}
//@formatter:on
}
ForwardingResponseBody.java
@SuppressWarnings(“资源”)
抽象类转发器
扩展响应体{
受保护的抽象ResponseBody ResponseBody();
//@formatter:off
@重写公共媒体类型contentType(){return responseBody().contentType();}
@重写公共长contentLength(){return ResponseBy().contentLength();}
@重写公共BufferedSource(){return ResponseBy().source();}
//@formatter:on
//@formatter:off
@重写公共void close(){super.close();}
//@formatter:on
}
ForwardingBufferedSource.java
抽象类转发BufferedSource
实现BufferedSource{
受保护的抽象BufferedSource BufferedSource();
//@formatter:off
@重写公共缓冲区(){return bufferedSource().Buffer();}
@重写公共布尔耗尽()抛出IOException{return bufferedSource().Expended();}
@重写公共void require(最终长字节数)抛出IOException{bufferedSource().require(字节数);}
@重写公共布尔请求(最终长字节数)引发IOException{return bufferedSource().request(字节数);}
@重写公共字节readByte()引发IOException{return bufferedSource().readByte();}
@Override public short readShort()抛出IOException{return bufferedSource().readShort();}
@Override public short readShortLe()抛出IOException{return bufferedSource().readShortLe();}
@重写公共int readInt()抛出IOException{return bufferedSource().readInt();}
@Override public int readIntLe()抛出IOException{return bufferedSource().readIntLe();}
@Override public long readLong()抛出IOException{return bufferedSource().readLong();}
@Override public long readLongLe()抛出IOException{return bufferedSource().readLongLe();}
@重写公共长readDecimalLong()引发IOException{return bufferedSource().readDecimalLong();}
@重写公共long readHexadecimalUnsignedLong()引发IOException{return bufferedSource().readHexadecimalUnsignedLong();}
@重写公共void skip(最终长字节数)抛出IOException{bufferedSource().skip(字节数);}
@重写public ByteString readByteString()引发IOException{return bufferedSource().readByteString();}
@重写公共ByteString readByteString(最终长字节数)引发IOException{return bufferedSource().readByteString(字节数);}
@重写公共int-select(最终选项)抛出IOException{return bufferedSource().select(选项);}
@重写公共字节[]readByteArray()引发IOException{return bufferedSource().readByteArray();}
@重写公共字节[]readByteArray(最终长字节计数)引发IOException{return bufferedSource().readByteArray(字节计数);}
@重写公共int-read(最终字节[]接收器)抛出IOException{return bufferedSource().read(接收器);}
@readFully重写公共void(最终字节[]sink)抛出IOException{bufferedSource()。readFully(sink);}
@重写公共整数读取(最终字节[]接收,最终整数偏移,最终整数字节计数)引发IOException{return bufferedSource().read(接收,偏移,字节计数);}
@readFully重写公共void(最终缓冲区接收器,最终长字节数)引发IOException{bufferedSource()。readFully(接收器,字节数);}
@覆盖公共长读入(最终接收器)t
{"foo":1,###"bar":2}
Content type:   application/json  
Content length: -1  
Content:        {"foo":1,###"bar":2}