Skip to content

@RequestMapping should support accept header and url extension [SPR-6497] #11163

@spring-projects-issues

Description

@spring-projects-issues

Paul Bakker opened SPR-6497 and commented

Sometimes it's necessary to execute different code to setup a model for a different type of content.
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.

@RequestMapping(value = "/list.html") //Doesn't work for accept header!
public ModelAndView listHTML(HttpServletRequest request) {

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

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

    return mav;
}

There seems to be no easy way at this moment to apply this. Of course you could use the headers parameter in the @RequestMapping, 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.

JAX-RS has a @Produces and @Consumes annotation which does the job perfectly. Wouldn't we need something similar in Spring?


Affects: 3.0 RC2

Issue Links:

3 votes, 3 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions