wood burning stoves*
The moose likes Other Application Frameworks and the fly likes Spring and binary responses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Other Application Frameworks
Bookmark "Spring and binary responses" Watch "Spring and binary responses" New topic
Author

Spring and binary responses

Stu Thompson
Hooplehead
Ranch Hand

Joined: Jun 14, 2006
Posts: 136
Hi all,

I have put together a simple HttpServlet that (on HTTP GET) interprets the query string, performs a little magic, and then returns binary data. It is a proof-of-concept prototype and works well.

Now I want to bring it into my Spring-based application properly. But I cannot seem to figure out how that is done! It seems like such a simple task, but my searches on "the google" have gotten me no where. Clearly I am missing a basic concept.

Could someone please point me in the correct direction?

Other potentially important details: Java 5, Spring 2, Hibernate 3, Tomcat 5.5.

tia,

Stu


"This is not to say that design is unnecessary. But after a certain point, design is just speculation." --Philip Chu
Chris Stehno
Ranch Hand

Joined: Feb 26, 2001
Posts: 180
I am assuming that your servlet adds the binary data to the response using getOutputStream(). If that is the case you can create a spring Controller that does the same thing. If your Controller does not return a ModelAndView object (null) no view will be rendered... it will trust that you have done something to the response if you wanted anything done.

Depending on what you are trying to do, you could also implement your own View/ViewResolver to write out the binary data... this is a better approach if you will have multiple controllers that need their response data sent in this manner.

The Spring documentation has very good examples around their MVC framework and Controllers.

Hope this helps.


- Chris Stehno, SCPJ
Stu Thompson
Hooplehead
Ranch Hand

Joined: Jun 14, 2006
Posts: 136
Originally posted by Chris Stehno:
I am assuming that your servlet adds the binary data to the response using getOutputStream(). If that is the case you can create a spring Controller that does the same thing. If your Controller does not return a ModelAndView object (null) no view will be rendered... it will trust that you have done something to the response if you wanted anything done.


Hi Chris,

My Spring application (Flash client remoting via AMF) does not utilize any Spring MVC stuff, so I plan to go with your first suggestion rather.

Correct, I am using getOutputStream(). Could you (or anyone reading this) please point me in the right direction regagarding "create a spring Controller that does the same thing"?

Thanks in advance and Happy New Year!

Stu
Stu Thompson
Hooplehead
Ranch Hand

Joined: Jun 14, 2006
Posts: 136
Just in case somebody searches for this thread, I've figured out how to do it! The gory details are on http://static.springframework.org/spring/docs/2.0.x/reference/mvc.html . Here is what I did:

1) My servlet now...
  • extends org.springframework.web.servlet.mvc.AbstractController instead of javax.servlet.http.HttpServlet
  • implements handleRequestInternal(..) instread of doGet(..), which returns null. Everything else in my method is exactly the same.

  • 2) my web.xml defines a <servlet> with the class org.springframework.web.servlet.DispatcherServlet and has an appropriate <servlet-mapping> definition
    3) my spring app configuration file, {servlet-name}-servlet.xml, has two beans:
  • the first is for org.springframework.web.servlet.handler.SimpleUrlHandlerMapping which has a 'mappings' property configured to map URLs to my class below
  • the second is my custom AbstractController class


  • Hope this helps someone someday!

    Stu
    [ January 23, 2007: Message edited by: Stu Thompson ]
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Spring and binary responses