连接声明了多个名为connectionLifecycleInterceptors的JSON字段

kxxlusnw  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(205)

I am trying to query a database and then convert the resultset obtained into json using the google gson library.While trying todo so, i am getting the error:
class com.mysql.jdbc.JDBC4Connection declares multiple JSON fields named connectionLifecycleInterceptors
I have tried to look for the connectionLifecycleInterceptors variable to change the name however i cant.Other questions in this context are not refering only to superclasses and subclass,while the error i am getting,i suppose comes from the database resultset.

public String convertToGson(ResultSet results)  throws Exception 
  { //code to convert resultset to gson
    Gson gson = new Gson();
    String gsonstring = gson.toJson(results);///it indicates the error is here
    return gsonstring;
   }

here is the error: java.lang.IllegalArgumentException: class com.mysql.jdbc.JDBC4Connection declares multiple JSON fields named connectionLifecycleInterceptors at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:172) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:102) at com.google.gson.Gson.getAdapter(Gson.java:458) at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:56) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:127) at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:245) at com.google.gson.Gson.toJson(Gson.java:704) at com.google.gson.Gson.toJson(Gson.java:683) at com.google.gson.Gson.toJson(Gson.java:638) at com.google.gson.Gson.toJson(Gson.java:618) at sample.QueryCentre.convertToGson(QueryCentre.java:91) at sample.QueryCentre.showavailablerooms(QueryCentre.java:72) at sample.ServerSide.main(ServerSide.java:51)

yhqotfr8

yhqotfr81#

ResultSet不是常规的POJO,您需要对其进行迭代并手动创建POJOMap示例。在此转换之后,您可以将这些示例的列表传递给序列化进程。
伪代码:

List<Map<String, Object>> rows = new ArrayList<>();
while (results.next()) {
    Map<String, Object> mapForRow = createMapFrom(result);
    rows.add(mapForRow);
}
String json = gson.toJson(rows);

有用的方法:

  • 获取元数据
  • 下一个

另请参阅:

相关问题