I'm a little bit confused about how I should implement this in Spring Web MVC 3.2.
What I want to do :
- page.json -> json response
- page.xml->xml response
- page.html -> FreeMarker template to generate html
- I want to ignore the "Accept" header since the extension is used
My controller
Code:
@Controller
public class APIController {
//specific mapping for html to avoid @ResponseBody
@RequestMapping(value="/page.html", method=RequestMethod.GET)
public String handlePageHtmlGet(@RequestParam String data, ModelMap model, HttpServletResponse response){
//...
return "page"; //name of the view
}
@RequestMapping(value="/page", method=RequestMethod.GET)
@ResponseBody
public PageResponse handlePageGet(@RequestParam String data, HttpServletResponse response){
//...
return pageResponse;
}
}
Note : PageResponse is just a regular POJO with Xml annotation.
My dispatcher-servlet.xml
Code:
<mvc:annotation-driven/>
<!-- Freemarker config -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/view/"/>
</bean>
<bean id="freeMarkerViewResolver"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true"/>
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
<property name="contentType" value="text/html;charset=UTF-8"/>
</bean>
This is working but here are my questions:
- How to restrict the supported extensions (If I try page.atom (or anything else that is not supported) an XML representation is returned)
- What is the purpose of ContentNegotiationManager in Spring 3.2?