简体   繁体   中英

How to retrieve json object from database in java

I tried getting json object using jackson but getting exception like this:

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

Here is the java code:

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

Here's the json sample in database:

{"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}}}

The database has only one column with json data.

You have a version incompatibility between jackson-core (streaming, low-level encoder/decoder) and jackson-databind (object mapping). Most likely you have 2.6 of jackson-databind , but some earlier version of jackson-core . Minor versions of these components must match; or at very least jackson-databind minor version can not be later that jackson-core version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM