Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@ResponseBody results in EofException

I have a Spring MVC controller that's returning a snippet of HTML (as a String) via the @ResponseBody annotation. Here's a look at the method signature:

@RequestMapping(value="/weather", method=RequestMethod.GET, produces="text/html")
@ResponseBody
public String getWeatherForcast(HttpServletResponse response)

When running the application from an embedded Jetty instance, I get the expected HTML response , however, the app server logs throw the following stack trace for every request:

org.eclipse.jetty.io.EofException
        at org.eclipse.jetty.server.HttpOutput.flush(HttpOutput.java:137)
        at ch.qos.logback.access.servlet.TeeServletOutputStream.flush(TeeServletOutputStream.java:85)
        at sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:297)
        at sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:141)
        at java.io.OutputStreamWriter.flush(OutputStreamWriter.java:229)
        at org.springframework.util.StreamUtils.copy(StreamUtils.java:107)
        at org.springframework.http.converter.StringHttpMessageConverter.writeInternal(StringHttpMessageConverter.java:106)
        at org.springframework.http.converter.StringHttpMessageConverter.writeInternal(StringHttpMessageConverter.java:40)
        at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
        at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148) ...

Having looked at the Jetty code, it's throwing this exception because the socket connection on the request has been closed.

That said, I've noticed if I instead I work directly with the Response object (snippet below) I get the same results but without the EofException.

@RequestMapping(value="/weather", method=RequestMethod.GET, produces="text/html")
public void getWeatherForcast(HttpServletResponse response) {     
     ....

response.getWriter().write(xpathTemplate.evaluateAsString("/rss/channel/item/description",result));
   response.flushBuffer();
}

I'm wondering why the Spring @ResponseBody approach would cause a premature socket close and if there's anyway to overcome this.

like image 959
JJensL Avatar asked Nov 11 '22 20:11

JJensL


1 Answers

I figured this out. I was using the Maven Cargo plugin (version 1.3.3) to run my embedded Jetty instance. Version 1.3.3 of Cargo using a release candidate version of the embedded Jetty container (v 9.0.0-RC0).

I tried upgrading the Cargo plugin to version 1.4.4 which uses Jetty 9.0.5 and the EofException went away.

like image 124
JJensL Avatar answered Nov 15 '22 02:11

JJensL