当前位置: 首页 > 知识库问答 >
问题:

Jackson.DataBind.JsonMappingException:已调用getWriter()

赫连琦
2023-03-14

使用jackson回写响应时发生异常。在网上找不到任何解决方案。我不确定错误的原因是什么。尝试刷新响应,但仍然是相同的错误

import com.In10s.DBProperties.OracleDBConnection;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONObject;

public class GetTable extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        JSONObject jsonResponse = new JSONObject();

        try {
            JSONArray data = new JSONArray();
            Connection con = OracleDBConnection.getConnection();
            String query = "Select * from CREDIT_CARD_TRANSACTIONS";
            Statement st = con.createStatement();
            ResultSet rSet = st.executeQuery(query);

            List<Map<String, Object>> result = new ArrayList<>();// JDK7++
            while (rSet.next()) {
                Map<String, Object> row = new HashMap<>();
                row.put("CARD_NUMBER", rSet.getString("CARD_NUMBER"));
                row.put("FIRST_NAME", rSet.getString("FIRST_NAME"));
                row.put("OPENING_BALANCE", rSet.getString("OPENING_BALANCE"));
                row.put("WITHDRAWAL", rSet.getString("WITHDRAWAL"));
                row.put("DEPOSIT", rSet.getString("DEPOSIT"));
                row.put("CLOSING_BAL", rSet.getString("CLOSING_BAL"));
                row.put("TXDATE", rSet.getString("TXDATE"));
                row.put("USAGE_TYPE", rSet.getString("USAGE_TYPE"));
                result.add(row);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        ObjectMapper mapper = new ObjectMapper();

        try {
            response.getWriter().write(mapper.writeValueAsString(response));
            response.getWriter().flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

}

下面是堆栈跟踪:

com.fasterxml.jackson.databind.JsonMappingException: getWriter() has already been called for this response (through reference chain: org.apache.catalina.connector.ResponseFacade["outputStream"])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:177)
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:190)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:671)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:156)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3383)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2777)
    at com.In10s.getTable.GetTable.doGet(GetTable.java:64)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IllegalStateException: getWriter() has already been called for this response
    at org.apache.catalina.connector.Response.getOutputStream(Response.java:644)
    at org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:196)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:536)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:663)
    ... 28 more

共有1个答案

贺福
2023-03-14

您正在尝试将HttpServletResponse转换为JSON,并将其发送到HttpServletResponse。您希望将result列表转换为JSON:

response.getWriter().write(mapper.writeValueAsString(result));
 类似资料:
  • 在过去的几天里,我一直在使用这段代码,直到今天,它一直工作得很好,但由于某种原因,异步任务已经停止调用doInBackground方法。我尝试了这里建议的解决方案,Android SDK AsyncTask doInBackground没有运行(子类),但我得到了相同的结果。onPreExecute方法被调用,但我只剩下加载对话框。有没有人经历过类似的事情。我已经附上了我的代码副本。MyAsncT

  • 我正在尝试使用以下代码段在浏览器中打开pdf文件: 我收到以下错误: 我还尝试使用以下方式关闭: 有人能帮我吗?

  • 尝试在和中捕获它,如 并且我在中有。

  • 问题内容: 我想将日志记录添加到Servlet中,因此我创建了Filter,该过滤器应显示请求并转到Servlet。但不幸的是,我遇到了例外: 因此,要解决此问题,我发现了Wrapper的解决方案,但它不起作用。我还能在代码中使用/更改什么?有任何想法吗? [MyHttpServletRequestWrapper] [MyFilter] 问题答案: 看起来restlet框架已经调用了Request

  • 我正在用Spring做一个项目,我有这个问题,我谷歌了错误信息,我找到了解决方案,甚至所有关于这个问题的帖子 有人能帮忙吗?

  • 我两天前才开始学习react,但我对react的方法感到很困难,我只知道使用参数(如果想根据以前的状态更改状态),并且在状态更改后立即执行回调参数(如果这个错误,请纠正我),所以我只是更改数组内容(我使用JavaScript的)并且我希望它在状态更改后立即呈现,它正在更改但延迟了,它只在我再单击一次后呈现,但render方法会被调用以获得任何senpai。谢谢帮助。 句柄点击更改以根据我的按钮“o