有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

jackson如何在java中从数据库中检索json对象

我尝试使用jackson获取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数据


共 (1) 个答案

  1. # 1 楼答案

    jackson-core(流式传输,低级编码器/解码器)和jackson-databind(对象映射)之间存在版本不兼容。最有可能的是jackson-databind的2.6版本,但是jackson-core的一些早期版本。这些组件的次要版本必须匹配;或者至少jackson-databind次要版本不能晚于jackson-core版本