I'm encountering an issue while using a custom Feign logger in my Java project. The logger successfully logs request and response data in a specific format, but I'm having trouble converting this format (request) to JSON and dealing with null responses. Here's a snippet of the log format:
request:
--18c87660f01
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary
11
--18c87660f01
Content-Disposition: form-data; name="refId"
Content-Type: text/plain; charset=UTF-8
6951c484-06f1-4011-b9fe-2b320ed980c3
--18c87660f01
Content-Disposition: form-data; name="title"
Content-Type: text/plain; charset=UTF-8
test.txt
--18c87660f01
Content-Disposition: form-data; name="lang"
Content-Type: text/plain; charset=UTF-8
I'm looking for guidance on how to convert this format to JSON, ignoring any File objects. Additionally, when the logAndRebufferResponse method is executed, it returns null:
BookResponse response = service.book(bookReq); // The service does not return a null object
Here's the relevant code from my custom Feign logger:
public class FileFeignLogger extends Logger {
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(FileFeignLogger.class);
@Autowired
private LogService logService;
@Value("${book.log.directory}")
private String uploadDirectory;
@Value("${endpoint}")
private String endpoint;
@Override
protected void logRequest(String configKey, Level logLevel, Request request) {
InputStream inputStream = new ByteArrayInputStream(request.body());
System.out.println(getStr(inputStream));
}
@Override
protected Response logAndRebufferResponse(String configKey, Level logLevel, Response response, long elapsedTime)
throws IOException {
System.out.println(getStr(response.body().asInputStream()));
//print:
//{"id":"276","refId":"94a2ff55-c28f-49bf-98eb-f16844224d24","status":101,"lastStatusUpdateOn":"24/12/2023 08:20:01"}
return response;
}
private String getStr(InputStream inputStream) {
try {
Path tempFile = Paths.get("D:\\development\\log.txt");
Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING);
return new String(Files.readAllBytes(tempFile));
} catch (IOException e) {
e.printStackTrace();
return "EX";
}
}
@Override
protected void log(String configKey, String format, Object... args) {
}
Translate the request into JSON format, and when the service.book(bookReq) function is called, it should return a non-null object.