I am working on spring based rest service and looking for the wy to validate custom request header for some methods and not the others. Could You advice what is the best way to achieve this please? Is it possible to achieve it e.g. via custom annotations?:
@RequestMapping(value = "/url", method = RequestMethod.GET)
@ResponseBody
@validateHeader(name=headerName) <--??
public DTO method() {
...
return DTO;
}
It's a little unclear what you are asking. If you're looking for your handler method to only handle requests that contain a specific header, you can use the
@RequestMapping
for that as well.RequestMapping
has aheaders
attribute where you can specifyUsing it like this
You can also specify that each header have a specific value (from the javadoc)
The javadoc is very descriptive, you can also specify if you want your handler to handle requests that don't contain a specific header. Go through the javadoc.