[英]How to retrieve json object from database in java
我尝试使用杰克逊获取json对象,但得到如下异常:
SEVERE: Servlet.service() for servlet [JsonProcessor] in context with path [/JJS2] threw exception [Servlet execution threw an exception] with root cause
java.lang.NoSuchMethodError: com.fasterxml.jackson.core.JsonGenerator.setCurrentValue(Ljava/lang/Object;)V
这是Java代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
JSONObject jsonResponse = new JSONObject();
List<Map<String, Object>> result = new ArrayList<>();// JDK7++
try {
JSONArray data = new JSONArray();
Connection con = OracleDBConnection.getConnection();
String query = "Select * from JJS";
Statement st = con.createStatement();
ResultSet rSet = st.executeQuery(query);
while (rSet.next()) {
Map<String, Object> row = new HashMap<>();
row.put("JSON_Diagram", rSet.getString("JSON_INFO"));
result.add(row);
}
} catch (Exception e) {
e.printStackTrace();
}
ObjectMapper mapper = new ObjectMapper();
try {
response.getWriter().write(mapper.writeValueAsString(result));
response.getWriter().flush();
} catch (Exception e) {
e.printStackTrace();
}
}
这是数据库中的json示例:
{"cells":
[{"type":"basic.Rect","position":{"x":-2,"y":33},
"size":{"width":71,"height":625},"angle":0,"isInteractive":false,
"id":"a55845b6-b753-42f0-b361-f0fcd3eaa611","z":1,
"attrs":{"rect":{"fill":"#EEEEEE","stroke":"#008B8B","stroke-width":2},
".":{"magnet":false}}}
该数据库只有一列包含json数据。
您在jackson-core
(流,低级编码器/解码器)和jackson-databind
(对象映射)之间存在版本不兼容。 您很可能拥有2.6的jackson-databind
,但有一些早期版本的jackson-core
。 这些组件的次要版本必须匹配; 或至少jackson-databind
次要版本不能晚于jackson-core
版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.