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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With