Results 1 to 6 of 6

Thread: spring mvc content type

  1. #1
    Join Date
    Jun 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Default spring mvc content type

    I would like to write a spring mvc controller method that directly writes to the servletOutputStream. I assume that this can be done by simply having an OutputStream object as a parameter to my controller method. but how would I set the contentType and other http header settings (content length etc.) The reason I need this I would prefer to no have to build some kind of crazy mock objects to write unit tests for this method.

    Is there any spring mvc object that would allow me to manipulate the http headers without using a HttpServletResponse object in my code?

  2. #2
    Join Date
    Jun 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Default

    Anybody have any ideas? please? I just need the ability to set the content type of a response without using the servlet response object as a parameter to my web method.

  3. #3
    Join Date
    May 2005
    Posts
    288

    Default

    You want to achieve something without the one object you can achieve it with. How is that supposed to work? On top of that you don't need it because of some given restraints, but only for the sake of not having to create a mock object in your tests. IMHO, simplifying tests is not a valid reason for disfiguring production code.

  4. #4
    Join Date
    Jun 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Default consistency

    You may not think so, but in every other area, the spring team seems to have done great effort for exactly that aim. Why else create the Model object to access the servlet request attributes without doing it on a request object directly? Why provide a mechanism to retrieve individual parameters (via the @RequestParam annotation) without a request object? Why provide your own SessionStats stats object when a HttpSession is already available via the spec? The answer to all of these questions is that mocking up a Servlet spec object to pass to a controller method violates the IoC concept. The framework should manipulate the servlet requests/responses not the controller method. Otherwise there is no separation between the controllers and the web layer at all.

  5. #5
    Join Date
    Jun 2008
    Location
    Springfield, MO
    Posts
    21

    Default

    There is a difference in simplifying access to commonly used objects and simplifying the creation of tests.

    There is not a lot of setup to creating an org.springframework.mock.web.MockHttpServletRespon se object. In fact it's as difficult as:
    Code:
    HttpServletResponse response = new MockHttpServletResponse();
    You can then set your content type, headers... and have access to the output stream. I could potentially see a little bit of value in being able to get the OutputStream directly as a method parameter, but then you'd have to have at a minimum two other parameters to be able to set the content type and headers.

  6. #6
    Join Date
    May 2005
    Posts
    288

    Default

    Usually, a view and not the controller deals with the response. Accessing the response inside a controller should rather be the exception and not the norm with Spring MVC, methinks.
    Abstracting away parts of the request like the @requestParam can add value, like converting types, I18N and so forth. I still fail to see, what added value you would get from passing the outputstream, headers and content type as discrete objects instead of using HtppServletResponse.
    The model is not an abstraction of the request attributes, as there are views, that don't deal with the request at all. Mind you, I've written view classes, where the model attributes were injected with setters.

    Just my 0.02€

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •