Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-8020

Ease creating Location headers in REST controllers


    • Type: New Feature
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 3.0.5, 3.1 M1
    • Fix Version/s: 3.1 RC2
    • Component/s: Web
    • Labels:
    • Last commented by a User:


      With it's annotation model Spring MVC provides a convenient way to write REST based server side components. Unfortunately creating fully-qualified URLs for the Location header (for POST requests especially) requires us to have HttpServletRequest and HttpServletResponse in the controller methods signature just to hand it to a helper method to copy the static part of the request URL (everything up to the servlet context) into a String and piping it back to the response.

      So as HttpHeaders already has a setLocation(URI uri) method, I wonder whether Spring MVC could simply "expand" this URI to prepend the path up to the servlet context in case the URI does not start with a protocol string. Beyond that, having a @ResponseHeaders annotation which allows you to bind the response headers to a controller method parameter of type HttpHeaders would round of the support. This way a controller could look something like this:

      public class CustomerController {
        private static final String CUSTOMERS = "/customers";
        private static final String CUSTOMER = CUSTOMERS + "/{id}";
        @RequestMapping(value = CUSTOMERS, method = RequestMethod.POST)
        public void createCustomer(@RequestBody Customer customer, @ResponseHeaders HttpHeaders headers) {
          Customer result = repository.save(customer);
          headers.setLocation(new UriTemplate(CUSTOMER).expand(result.getId()));
        @RequestMapping(value = CUSTOMER, method = RequestMethod.GET)
        public @ResponseBody Customer customer(@PathVariable("id") Long id) {
          return repository.findOne(id);




            • Assignee:
              rstoya05-aop Rossen Stoyanchev
              olivergierke Oliver Gierke
              Last updater:
              Trevor Marshall
            • Votes:
              3 Vote for this issue
              5 Start watching this issue


              • Created:
                Days since last comment:
                6 years, 24 weeks, 4 days ago