Results 1 to 3 of 3

Thread: Suggested way to add support for file extensions in the request URI in Spring 3.2

  1. #1
    Join Date
    Jan 2012
    Posts
    11

    Default Suggested way to add support for file extensions in the request URI in Spring 3.2

    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?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You are confusing 2 things here, request mapping and result mapping.

    If you only want to accept those extensions then explicitly map them (/page will by default also map /page.*).

    Result mapping is done on different levels, where the file extension has precedence (by default). The result mapping is done/configured by the ContentNegotiationManager (I suggest a read of the reference guide and the javadocs of that class).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jan 2012
    Posts
    11

    Default

    So I should do my mapping like that:
    Code:
    @RequestMapping(value="/page{extension:\\.(?:json|xml)}", method=RequestMethod.GET)
    I read the ContentNegotiationManager reference guide and the javadocs and I'm ok with the default strategy/configuration.

    I'm confused because a class like ContentNegotiatingViewResolver(result mapping) has a list of mediaTypes and a method setUseNotAcceptableStatusCode. So I thought that the extension restriction could be done at that level.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •