I have a Spring 3.0 application, with an Web Controller Method. This method normaly return a file in the http response, therefore I used the return type org.springframework.http.HttpEntity. But now there is a second requirement: if the file is larger than 1MB and it is after 10 o’clock, a HTML page should be displayed.
So my problem is, that the method sometimes should be return a HttpEntity<byte[]> and sometimes a ModelAndView. But how can one have this two different kinds of a return type?
(Ok the requirement is not 10 o’clock, it is much more complicated, but the point is, that this dessicion can be made only in the controller.)
(This application uses classic JSPX for rendering HTML paged.)
Its too easy (sorry for the question): one could define the method with return type
Object, so one could return instances ofModelAndVieworHttpEntity.This works because the
AnnotationMethodHandlerAdapter#getModelAndViewtakes the return value as anObjectand then has a if-then-else cascade with an lot ofinncstanceofstatements to determine the concreate instance type.If one feels that the return type
Objectis too common, then one could define its own class (compound-class), witch contains aModelAndVieworHttpEntityin two different fields. And then one have to write a customModelAndViewResolver.This custom
ModelAndViewResolvertake the compound-object andModelAndVieworwebRequestlikeAnnotationMethodHandlerAdapter#handleHttpEntityResponsedoes and then returns null