12

I am aware of @RequestMapping annotation which is used in Spring MVC based application.

I came across this piece of code:

@RequestMapping(method = POST, params = {"someParam"})

I understood the method. However I don't know what params means? Before this I never had seen anything which passed params to this annotation.

Can anyone help in understanding this?

CuriousMind
  • 8,301
  • 22
  • 65
  • 134

1 Answers1

15

Your example means that the parameter someParam must be present in the request. This is used to narrow down the matching methods for the given request.

See the documentation: RequestMapping#params

Codo
  • 75,595
  • 17
  • 168
  • 206
  • Thanks so much for your reply. Is this technique still prevalent? – CuriousMind Aug 23 '16 at 17:39
  • 1
    I can't really tell. If I design a new API, I don't use. Instead I use different URLs to distinguish different method. However, if you have to reimplement an existing API or split a too overloaded method into two, it might come in handy. – Codo Aug 23 '16 at 17:43
  • Thanks again for your response, need to do some more study on this. – CuriousMind Aug 23 '16 at 17:45
  • It definitely makes sense in quite a few cases. The simplest example is fetch all vs fetch one. Fetch all could use `@GetMapping(params = "!id") public fetch() {...}` and fetch one could use `@GetMapping(params = "id") fetch(String id) {...}`. The other option would be a single method which supports both cases using an if-test which is less clean imo. – Natan Cox Jul 22 '22 at 10:12