Announcement Announcement Module
Collapse
No announcement yet.
@ResponseBody throws java.lang.IllegalStateException: getOutputStream() Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @ResponseBody throws java.lang.IllegalStateException: getOutputStream()

    I have a simple Spring MVC Roo generated app with apache tiles. I'm trying to read an xml file and return as json wrapped in a Response POJO but I'm getting java.lang.IllegalStateException: getOutputStream() has already been called for this response.

    Any help would be greatly appreciated. Thank you.


    Code:
    @RequestMapping(value="/json/fromxml/{filename}", method=RequestMethod.GET)
    public @ResponseBody Response readXml(@PathVariable("filename") String filename)
    {
        Response r = new Response();
        String xml ="";
        try
        {
            InputStream is = servletContext.getResourceAsStream("/xml/"+filename+".xml");
            if (is != null) {
                Writer writer = new StringWriter();
    
                char[] buffer = new char[1024];
                try {
                    Reader reader = new BufferedReader(
                            new InputStreamReader(is, "UTF-8"));
                    int n;
                    while ((n = reader.read(buffer)) != -1) {
                        writer.write(buffer, 0, n);
                    }
                } finally {
                    is.close();
                }
                xml = writer.toString();
            } else {        
                xml = "";
            }
    
    
        }catch(Exception e)
        {
            r.setStatus(Response.STATUS.ERROR);
            return r;
        }
        JSONObject xmlJSONObject = null;
        try {
            xmlJSONObject = XML.toJSONObject(xml);
        } catch (JSONException e) {
            r.setStatus(Response.STATUS.ERROR);
            return r;
        }
        r.setStatus(Response.STATUS.OK);
        r.setBody(xmlJSONObject);
        return r;
    }

    Here's the stack trace:

    java.lang.IllegalStateException: getOutputStream() has already been called for this response at org.apache.catalina.connector.Response.getWriter(R esponse.java:611) at org.apache.catalina.connector.ResponseFacade.getWr iter(ResponseFacade.java:198) at org.apache.jasper.runtime.JspWriterImpl.initOut(Js pWriterImpl.java:125) at org.apache.jasper.runtime.JspWriterImpl.flushBuffe r(JspWriterImpl.java:118) at org.apache.jasper.runtime.PageContextImpl.release( PageContextImpl.java:188) at org.apache.jasper.runtime.JspFactoryImpl.internalR eleasePageContext(JspFactoryImpl.java:118) at org.apache.jasper.runtime.JspFactoryImpl.releasePa geContext(JspFactoryImpl.java:77) at org.apache.jsp.WEB_002dINF.layouts.default_jspx._j spService(default_jspx.java:83) at org.apache.jasper.runtime.HttpJspBase.service(Http JspBase.java:70) at javax.servlet.http.HttpServlet.service(HttpServlet .java:717) at org.apache.jasper.servlet.JspServletWrapper.servic e(JspServletWrapper.java:386) at org.apache.jasper.servlet.JspServlet.serviceJspFil e(JspServlet.java:313) at org.apache.jasper.servlet.JspServlet.service(JspSe rvlet.java:260) at javax.servlet.http.HttpServlet.service(HttpServlet .java:717) at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206) at org.apache.catalina.core.ApplicationDispatcher.inv oke(ApplicationDispatcher.java:646) at org.apache.catalina.core.ApplicationDispatcher.pro cessRequest(ApplicationDispatcher.java:436) at org.apache.catalina.core.ApplicationDispatcher.doF orward(ApplicationDispatcher.java:374) at org.apache.catalina.core.ApplicationDispatcher.for ward(ApplicationDispatcher.java:302) at org.apache.tiles.servlet.context.ServletTilesReque stContext.forward(ServletTilesRequestContext.java: 241) at org.apache.tiles.servlet.context.ServletTilesReque stContext.dispatch(ServletTilesRequestContext.java :222) at org.apache.tiles.renderer.impl.TemplateAttributeRe nderer.write(TemplateAttributeRenderer.java:44) at org.apache.tiles.renderer.impl.AbstractBaseAttribu teRenderer.render(AbstractBaseAttributeRenderer.ja va:106) at org.apache.tiles.impl.BasicTilesContainer.render(B asicTilesContainer.java:670) at org.apache.tiles.impl.BasicTilesContainer.render(B asicTilesContainer.java:690) at org.apache.tiles.impl.BasicTilesContainer.render(B asicTilesContainer.java:644) at org.apache.tiles.impl.BasicTilesContainer.render(B asicTilesContainer.java:627) at org.apache.tiles.impl.BasicTilesContainer.render(B asicTilesContainer.java:321) at org.springframework.web.servlet.view.tiles2.TilesV iew.renderMergedOutputModel(TilesView.java:124) at org.springframework.web.servlet.view.AbstractView. render(AbstractView.java:250) at org.springframework.web.servlet.DispatcherServlet. render(DispatcherServlet.java:1047) at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:817) at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:719) at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:644) at org.springframework.web.servlet.FrameworkServlet.d oGet(FrameworkServlet.java:549) at javax.servlet.http.HttpServlet.service(HttpServlet .java:617) at javax.servlet.http.HttpServlet.service(HttpServlet .java:717) at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206) at org.springframework.web.filter.HiddenHttpMethodFil ter.doFilterInternal(HiddenHttpMethodFilter.java:7 7) at org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206) at org.springframework.web.filter.CharacterEncodingFi lter.doFilterInternal(CharacterEncodingFilter.java :88) at org.springframework.web.filter.OncePerRequestFilte r.doFilter(OncePerRequestFilter.java:76) at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:235) at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102) at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298) at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:859) at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489) at java.lang.Thread.run(Thread.java:662)

  • #2
    Have this same problem, found the cause?

    Comment

    Working...
    X