<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=paulb" rel="nofollow">Paul Bakker</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-6497?redirect=false" rel="nofollow">SPR-6497</a></strong> and commented</p>
<p dir="auto">Sometimes it's necessary to execute different code to setup a model for a different type of content.<br>
For example; a list of employees should be returned directly for rendering HTML, but should be wrapped in a JAXB class for rendering XML, as in the following example.</p>
<div class="highlight highlight-source-java notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="@RequestMapping(value = &quot;/list.html&quot;) //Doesn't work for accept header!
public ModelAndView listHTML(HttpServletRequest request) {

    ModelAndView mav = new ModelAndView();
    mav.addObject(&quot;employees&quot;, employeeDao.listEmployees());
    return mav;
}

@RequestMapping(value = &quot;/list.xml&quot;) //Doesn't work for accept header!
public ModelAndView listXML(HttpServletRequest request) {
    ModelAndView mav = new ModelAndView();
    mav.addObject(&quot;employees&quot;, new EmployeeList(employeeDao.listEmployees()));

    return mav;
}"><pre class="notranslate"><span class="pl-c1">@</span><span class="pl-c1">RequestMapping</span>(<span class="pl-s1">value</span> = <span class="pl-s">"/list.html"</span>) <span class="pl-c">//Doesn't work for accept header!</span>
<span class="pl-k">public</span> <span class="pl-smi">ModelAndView</span> <span class="pl-s1">listHTML</span>(<span class="pl-smi">HttpServletRequest</span> <span class="pl-s1">request</span>) {

    <span class="pl-smi">ModelAndView</span> <span class="pl-s1">mav</span> = <span class="pl-k">new</span> <span class="pl-smi">ModelAndView</span>();
    <span class="pl-s1">mav</span>.<span class="pl-en">addObject</span>(<span class="pl-s">"employees"</span>, <span class="pl-s1">employeeDao</span>.<span class="pl-en">listEmployees</span>());
    <span class="pl-k">return</span> <span class="pl-s1">mav</span>;
}

<span class="pl-c1">@</span><span class="pl-c1">RequestMapping</span>(<span class="pl-s1">value</span> = <span class="pl-s">"/list.xml"</span>) <span class="pl-c">//Doesn't work for accept header!</span>
<span class="pl-k">public</span> <span class="pl-smi">ModelAndView</span> <span class="pl-s1">listXML</span>(<span class="pl-smi">HttpServletRequest</span> <span class="pl-s1">request</span>) {
    <span class="pl-smi">ModelAndView</span> <span class="pl-s1">mav</span> = <span class="pl-k">new</span> <span class="pl-smi">ModelAndView</span>();
    <span class="pl-s1">mav</span>.<span class="pl-en">addObject</span>(<span class="pl-s">"employees"</span>, <span class="pl-k">new</span> <span class="pl-smi">EmployeeList</span>(<span class="pl-s1">employeeDao</span>.<span class="pl-en">listEmployees</span>()));

    <span class="pl-k">return</span> <span class="pl-s1">mav</span>;
}</pre></div>
<p dir="auto">There seems to be no easy way at this moment to apply this. Of course you could use the headers parameter in the <code class="notranslate">@RequestMapping</code>, but that doesn't work when a request is done without the accept header using an extension in the url instead (e.g. list.xml). When using an url extension as in the above example it doesn't work when only the accept header is set.</p>
<p dir="auto">JAX-RS has a <code class="notranslate">@Produces</code> and <code class="notranslate">@Consumes</code> annotation which does the job perfectly. Wouldn't we need something similar in Spring?</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 3.0 RC2</p>
<p dir="auto"><strong>Issue Links:</strong></p>
<ul dir="auto">
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398103877" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/11658" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/11658/hovercard" href="https://github.com/spring-projects/spring-framework/issues/11658">#11658</a> Add content-negotiation to RequestBody and ResponseBody processing (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398108565" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/12378" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/12378/hovercard" href="https://github.com/spring-projects/spring-framework/issues/12378">#12378</a> <code class="notranslate">@RequestMapping</code> should support alternative between a header and a url extension (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398113614" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13190" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13190/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13190">#13190</a> Spring MVC - Ability to use a Converter based on the Path Extension instead of Accep Header. (<em><strong>"is duplicated by"</strong></em>)</li>
<li><a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="398112762" data-permission-text="Title is private" data-url="https://github.com/spring-projects/spring-framework/issues/13057" data-hovercard-type="issue" data-hovercard-url="/spring-projects/spring-framework/issues/13057/hovercard" href="https://github.com/spring-projects/spring-framework/issues/13057">#13057</a> Content negotiation improvements (<em><strong>"is superseded by"</strong></em>)</li>
</ul>
<p dir="auto">3 votes, 3 watchers</p>
<p dir="auto"><strong><a href="https://jira.spring.io/secure/ViewProfile.jspa?name=baronludwig" rel="nofollow">Marc Ludwig</a></strong> opened <strong><a href="https://jira.spring.io/browse/SPR-4626?redirect=false" rel="nofollow">SPR-4626</a></strong> and commented</p>
<hr>
<p dir="auto"><strong>Affects:</strong> 2.5.2</p>
<p dir="auto"><strong>Attachments:</strong></p>
<ul dir="auto">
<li><a href="https://jira.spring.io/secure/attachment/13840/ConfigurableTestCase.zip" rel="nofollow">ConfigurableTestCase.zip</a> (<em>3.09 kB</em>)</li>
</ul>