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

时间:2014-09-27 11:50:24

标签: java json http-headers httpresponse struts-1

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

我能够编写一些行,直到现在它在{1}}处给出了下面代码中指向的行。

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

这是 strut-config xml

的一部分
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;
    }

这是我的stacktrace

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

1 个答案:

答案 0 :(得分:2)

根据您添加的评论我可以发送一定数量的数据但是当我超过它时会给我错误,它似乎是标题大小(在您的情况下是{{1} }})超出了最大限制(参考Maximum on http header values?可能会有帮助),问题似乎是服务器特定的。

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

您还必须关闭response.setHeader("X-JSON", jsonString);对象,如下所示:

writer