I have a Spring MVC-Controller which generates some some kind of Byte-Response, which is directly written to the Response-Outputstream. For this case I need a controller with this signature
@BodyResponse
@AuthorizedMethod(...)
public void createPdf() {
// doSomething on response
}
But when User is not authorized I want to show him Login-Form. So I need such a method signature
@AuthorizedMethod(...)
public ModelAndView createPdf() {
return new ModelAndView("login.jsp");
}
How can I handle this?
Regards,
Michael
For the specific case of a security check, usually the security framework would handle doing a redirect there automatically, unless you’re doing like some kind of ‘upgrade’ login.
For the general case of your question:
There is a special trap in the HandlerAdapter that disables view resolution if you read in the response and return null for the ModelAndView.
What this means is that if you just take in HttpServletResponse as a method parameter and write the bytes yourself, then return null, Spring will take no further action and commit the response.
Then in the case where you don’t want to write the bytes and instead want to return a view, just return a ModelAndView as normal.