I have a Spring MVC controller that returns an HTML fragment (like String) using the @ResponseBody annotation. Consider the method signature here:
@RequestMapping(value="/weather", method=RequestMethod.GET, produces="text/html")
@ResponseBody
public String getWeatherForcast(HttpServletResponse response)
When I start the application from the embedded Jetty instance, I get the expected HTML response, however the server application logs throw the following stack trace for each 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) ...
Looking at the Jetty code, he throws this exception because the socket connection on request was closed.
However, I noticed that instead I work directly with the Response object (snippet below). I get the same results, but without an 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 am wondering why the Spring @ResponseBody approach will close the premature socket and if it is anyway to overcome this.