gpt4 book ai didi

java - 在 Struts 中将 JSON 字符串写入 Printwriter 时出现 ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-12-02 05:25:31 24 4
gpt4 key购买 nike

我从数据库获取行的 List 并将其转换为 JSONstring 并将其写入 PrintWritter 以将输出作为 JSON 发送。

我能够写入一些行,直到现在它在下面代码中指向的行处给出了ArrayIndexOutOfBoundsException

Gson gson = new Gson();
String jsonString = gson.toJson(subAreaCriteriaList);
response.setHeader("X-JSON", jsonString);

PrintWriter writer = response.getWriter();
writer = response.getWriter();
writer.write(jsonString);//exception at this line
writer.flush();

这是我的操作方法

public Object doGetSubAreasifExist(BaseActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
try {
CurrentRoute route = new CurrentRoute();
String areaID = request.getParameter("areaID");
LoginService bbcService = (LoginService) Context.getInstance()
.getBean("LoginService");

DetachedCriteria subAreaCriteria = DetachedCriteria.forClass(CabArea.class);
subAreaCriteria.add(Restrictions.ne(CabArea.PROP_ID, Long.parseLong(areaID)));

List<CabArea> subAreaCriteriaList = bbcService.findAll(subAreaCriteria);

Gson gson = new Gson();
String jsonString = gson.toJson(subAreaCriteriaList);
response.setHeader("X-JSON", jsonString);

PrintWriter writer = response.getWriter();
writer = response.getWriter();
writer.write(jsonString);
writer.flush();
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

这是我的strut-config xml部分

<action path="/admin/cabsharing/getSubAreasifExist"
type="com.ihexa.common.admin.cabsharing.action.CabsharingAction"
name="actionForm">
</action>

这是我的堆栈跟踪

java.lang.ArrayIndexOutOfBoundsException
at java.lang.System.arraycopy(Native Method)
at org.apache.coyote.http11.InternalOutputBuffer.write(InternalOutputBuffer.java:701)
at org.apache.coyote.http11.InternalOutputBuffer.sendStatus(InternalOutputBuffer.java:438)
at org.apache.coyote.http11.Http11Processor.prepareResponse(Http11Processor.java:1626)
at org.apache.coyote.http11.Http11Processor.action(Http11Processor.java:958)
at org.apache.coyote.Response.action(Response.java:183)
at org.apache.coyote.Response.sendHeaders(Response.java:379)
at org.apache.catalina.connector.OutputBuffer.doFlush(OutputBuffer.java:314)
at org.apache.catalina.connector.OutputBuffer.close(OutputBuffer.java:274)
at org.apache.catalina.connector.Response.finishResponse(Response.java:493)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:317)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)

最佳答案

根据您添加的评论我可以发送一定数量的数据,但是当我超过时它会给我错误,它看起来像 header 大小(在您的情况下是 response.setHeader("X-JSON", jsonString);) 超出了最大限制(引用 Maximum on http header values? 可能会有所帮助),并且问题似乎是特定于服务器的。

如果您使用的是 Apache Tomcat 那么您可以引用 maxHttpHeaderSize in Apache Tomcat Configuration Reference修改conf/server.xml。在 server.xml 中设置 maxHttpHeaderSize 将解决您的问题。这是sample server.xml maxHttpHeaderSize 使用的文件。

您还必须关闭writer对象,如下所示:

writer.flush();
writer.close();

关于java - 在 Struts 中将 JSON 字符串写入 Printwriter 时出现 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26074351/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com