java.io.NotSerializableException : org. json.JSONObject

标签 java mysql rest

JDBCTemplate 和 MYSQL JSON

我正在编写一个休息服务,我需要在 mysql 表中添加一些细节。

我的 mysql 表作为列 area_json 作为 json 类型。

我从 rest 服务的 post 调用中得到了一个 pojo 对象,我尝试使用 jdbctemplate 之类的方式插入它

jdbcTemplate.update("insert into site(area_id,area_json) values(?,?)", area.getareaID(), area.getareaJson());

一旦我使用 post man 进行 post 调用,我会收到以下错误

Invalid argument value: java.io.NotSerializableException;
nested exception is java.sql.SQLException: Invalid argument value: java.io.NotSerializableException"

请帮忙

最佳答案

首先,确保使用不早于 v5.1.37 的 Connector/J 版本,其中添加了对 JSON 数据类型的支持,最好不早于 v5.1.40,它修复了一些与 JSON 相关的错误.但如果这对您来说是个问题,那么它充其量只是次要的。

在此之前,JdbcTemplate 需要了解您的 JSONObject 参数。 The particular method you are using记录变量参数,包括您的 JSONObject

arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type); may also contain SqlParameterValue objects which indicate not only the argument value but also the SQL type and optionally the scale

当它说PreparedStatement会猜的时候,它只能表示the two-arg version of PreparedStatement.setObject()哪些文件:

The JDBC specification specifies a standard mapping from Java Object types to SQL types. The given argument will be converted to the corresponding SQL type before being sent to the database.

Note that this method may be used to pass datatabase- specific abstract data types, by using a driver-specific Java type. If the object is of a class implementing the interface SQLData, the JDBC driver should call the method SQLData.writeSQL to write it to the SQL data stream. If, on the other hand, the object is of a class implementing Ref, Blob, Clob, NClob, Struct, java.net.URL, RowId, SQLXML or Array, the driver should pass it to the database as a value of the corresponding SQL type.

(强调已添加。)

JDBC 没有对 JSON 数据类型的内置支持。文档确实允许 Connector/J 提供与 JSON SQL 数据类型相对应的特定于驱动程序的类型,但仅仅因为 MySQL 和 Connector/J 支持 JSON 并不意味着您可以从架子上拉出一个随机的 JSON 表示对象并呈现它对他们来说,他们也根本没有提供或容纳特定于 JSON 的 Java 数据类型。因为 MySQL 似乎没有在其 Connector/J developer guide 中发布或引用 JDBC 扩展 API 的任何文档。 , 我倾向于怀疑它是否识别任何此类类型。

那么,您似乎应该依赖与直接使用 MySQL 的 SQL 方言编程时相同的技术:

In MySQL, JSON values are written as strings. MySQL parses any string used in a context that requires a JSON value, and produces an error if it is not valid as JSON. These contexts include inserting a value into a column that has the JSON data type and passing an argument to a function that expects a JSON value [...].

( MySQL 5.7 Reference Manual, section 11.6 )

也就是说,将您的 JSONObject 转换为 JSON 格式的 String,然后以这种方式将其呈现给您的模板。在检索时,预计必须执行相反的操作。

关于java.io.NotSerializableException : org. json.JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47894354/

相关文章:

java - 对于 Hibernate 中的 MySQL 6,我应该使用哪种方言?

c++ - 我需要在 C++ REST SDK 的什么地方调用 CoInitialize?

java - 如何打印我的 Java 对象而不得到 "SomeType@2f92e0f4"?

java - Ignite Marshaller 不使用二进制或优化的

sql - 查询宽度和高度,在同一个查询中一个记录大于另一个记录?

mysql - Perl/MySQL 关系查询

Spring Boot RestController - PostMapping - 将请求正文作为 InputStream 处理

php - PHP、重写和 http-verbs 上的 REST 实践

java - NoClassDefFoundError 无法解析基本模块中的数据绑定(bind)类

java - 如何为 Mac/Windows 捆绑 Java 应用程序?