Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I close the Writer parameter to my Spring 3 controller?

In Section 15.3.2.3 Supported handler method arguments and return types of the Spring 3.0 documentation, it says that a java.io.OutputStream or java.io.Writer can be specified as a parameter of a method annotated with @RequestMapping "for generating the response's content. This value is the raw OutputStream/Writer as exposed by the Servlet API." Is it the method's responsibility to close the writer before it finishes or should it remain open and some other Spring process will close it?

like image 883
LostHisMind Avatar asked Dec 10 '10 16:12

LostHisMind


People also ask

Is @RequestParam mandatory?

Method parameters annotated with @RequestParam are required by default. will correctly invoke the method. When the parameter isn't specified, the method parameter is bound to null.

What are the valid arguments to a Spring controller method?

The following are the supported method arguments: Request or response objects (Servlet API). Choose any specific request or response type, for example ServletRequest or HttpServletRequest . Session object (Servlet API): of type HttpSession .

What is the difference between request params and request body?

@RequestParam makes Spring to map request parameters from the GET/POST request to your method argument. @RequestBody makes Spring to map entire request to a model class and from there you can retrieve or set values from its getter and setter methods.

Which component helps controller send the data to view in Spring?

The DispatcherServlet The Controller takes the request and calls the appropriate service methods based on used GET or POST method. The service method will set model data based on defined business logic and returns view name to the DispatcherServlet.


1 Answers

It should leave it alone. As a general rule of thumb, if your code didn't open it, then it shouldn't close it, either.

In this case, the servlet container (not Spring) is responsible for flushing and closing all streams.

You could do it yourself, I doubt it would do any harm, but there's no need to do so.

like image 88
skaffman Avatar answered Sep 19 '22 23:09

skaffman