0

I need to store HTTP REST API (POST, GET, PATCH, etc.) requests and responses into a database entry (Column as BLOB), so that we can audit the requests and responses later.

As part of the incoming HTTP POST request, the DTO object is coming as request body. I can extract the JSON object as a request body.

How can I convert that JSON object to BLOB in Java?

MultiplyByZer0
  • 6,302
  • 3
  • 32
  • 48
Asif Billa
  • 821
  • 2
  • 15
  • 28
  • This question already has an answer https://stackoverflow.com/questions/7947871/convert-a-string-to-a-byte-array-and-then-back-to-the-original-string and https://stackoverflow.com/questions/36560223/how-do-i-convert-a-jsonobject-to-a-byte-array-and-then-convert-this-byte-array-t/36560611 – gbenga wale Jun 17 '17 at 21:34
  • 1
    This question is the same as https://stackoverflow.com/questions/17400497/how-to-convert-blob-to-string-and-string-to-blob-in-java – tima Jun 18 '17 at 02:20
  • @gbengawale He's asking for BLOB. Both your suggestions have nothing to do with blob. – Teddy Aug 11 '22 at 05:58

1 Answers1

0

Try this

String str = json.toString();
PreparedStatement ps1 = conn.prepareStatement("update table set blob=? where id=1");
Blob blob = conn.createBlob();
blob.setBytes(1, str.getBytes());
ps1.setBlob(1, blob);
ps1.executeUpdate();