Q:

Automatically add "headers" and "produces" attributes to my @RequestMapping

I implemented a REST API via Spring MVC. Here is an example of a mapping:
@RequestMapping(value = "/videos", method = RequestMethod.GET, headers = "Accept=application/json", produces = "application/json")

There are many of them, so I wonder if it's possible to factorize the headers and produces attributes, so that I don't have to specify them in each mapping, in order to lighten my code?
The best would be a custom annotation which automatically sets the two attributes, for example:
@JsonRequestMapping(value = "/videos", method = RequestMethod.GET)

But I haven't been able to implement such one...

A:

You can put @RequestMapping also on a class next to a method (see reference guide). If you want globally available attributes put a @RequestMapping on a class, this will be merged with the one on the method.
@Controller
@RequestMapping(headers = "Accept=application/json", produces = "application/json")
public class YourController { ... }

Then your method only contains the method and url.
@RequestMapping(value="/videos", method=RequestMethod.GET)
public Object someMethod(...) { ... }

You also might want to take a look at @RestController as that also configures some defaults for your controller. Like not needing a @ResponseBody anymore on your methods.
@RestController
public class YourController { ... }

