Let's say, I have a REST styled controller mapping
@RequestMapping(value="users", produces = {MediaType.APPLICATION_JSON_VALUE})
public List<User> listUsers(@ReqestParams Integer offset, @ReqestParams Integer limit, @ReqestParams String query) {
return service.loadUsers(query, offset, limit);
}
Serving JSON (or even XML) is not an issue, this is easy using ContentNegotation and MessageConverters
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="favorParameter" value="false" />
<property name="ignoreAcceptHeader" value="false" />
<property name="mediaTypes" >
<value>
html=text/html
json=application/json
xml=application/xml
</value>
</property>
</bean>
Now, I need to add support for PDF. Naturally, I want to use (Spring) MVC + REST as much as possible. Most examples I have found implement this with an explicit definition not using REST style, e.g.
@RequestMapping(value="users", produces = {"application/pdf"})
public ModelAndView listUsersAsPdf(@ReqestParams Integer offset, @ReqestParams Integer limit, @ReqestParams String query) {
List<User> users = listUsers(offset, limit, query); // delegated
return new ModelAndView("pdfView", users);
}
That works, but is not very comfortable because for every alternate output (PDF, Excel, ...) I would add a request mapping.
I have already added application/pdf
to the content negotation resolver; unfortunately any request with a suffix .pdf
or the Accept-Header application/pdf
were be responded with 406
.
What is the ideal setup for a REST/MVC style pattern to integrate alternate output like PDF?