2017-06-17 276 views
0

我需要将HTTP REST API(POST,GET,PATCH等)请求和响应存储到数据库条目中(列为BLOB),以便我们可以审核请求并稍后回应。如何将Json对象转换为java中的BLOB

作为传入的HTTP POST请求的一部分,DTO对象作为请求主体来了。我可以提取JSON对象作为请求主体。

如何将该JSON对象转换为Java中的BLOB?

+0

这个问题已经有了答案 https://stackoverflow.com/questions/7947871/convert-a-string-to-a-byte-array-and-then-back -to-the-original-string和https://stackoverflow.com/questions/36560223/how-do-i-convert-a-jsonobject-to-a-byte-array-and-then-convert-this-byte -array-t/36560611 –

+0

此问题与https://stackoverflow.com/questions/17400497/how-to-convert-blob-to-string-and-string-to-blob-in-java相同 – tima

回答

0

试试这个

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();