This short tutorial is about how to read the HTTP request header in the spring framework.
While refactoring a REST service, I came across code duplicates.
There were 5 similar methods, whereby only the RequestBody and the Return method differed minimally.
In this case, it’s a GET request which serializes an RDF record into different formats on request.
Now I wanted to convert these 5 methods into a single method.
With the @RequestHeader annotation, this is no problem.
 
 @RequestMapping(value = {"/rest/**"},
        method = RequestMethod.GET)
    public @ResponseBody ResponseEntity getContainer(@RequestHeader(value="Accept") String acceptHeader, HttpServletRequest request) {
        log.debug("Request to get container: {} in format: {}", path, acceptHeader);
        // dummy code to get path
        String path = "";
        // return rdf in requested format
        return new ResponseEntity<>(containerService.find(path).toString(acceptHeader), HttpStatus.OK);
    }
			