4

I would like to know what are the differences between Path and Variable in Spring MVC in the Controller class.

@RequestMapping("/home")
@RequestMapping(value = "/home")
@RequestMapping(path = "/home")

Base on the Spring Documentation Spring 5 Annotation Type RequestMapping the path is an alias for value, and the value is the alias for the path. I would like to know this 3 RequestMapping definitions and difference.

galmeriol
  • 461
  • 4
  • 14
Mani Zaeim
  • 49
  • 4

3 Answers3

2

There's no difference between @RequestMapping("/home") and @RequestMapping(value = "/home").But if you want to add some other parameter then you have to use,

@GetMapping(value = "/home/{ABC}", consumes = MediaType.ALL_VALUE)

because if write,

@GetMapping("/getTodayActivity/{millis}", consumes = MediaType.ALL_VALUE)

then it will compile error, so only want to use more parameter then you have to use "value"

chandrakant
  • 370
  • 3
  • 25
1

According to this there is no difference between @RequestMapping("/home") and @RequestMapping(value = "/home") when you are using to class level or method level.

However, you can use pass more than one variable with this usage @RequestMapping(value={"/method1","/method1/second"})

Eduard Grinchenko
  • 514
  • 1
  • 6
  • 28
Hatice
  • 874
  • 1
  • 8
  • 17
  • Reference should be official document or from reliable and wellknown source – Mạnh Quyết Nguyễn May 23 '18 at 06:40
  • Thanks. Could I need to update my answer because actually spring docs also says same thing ? This is an alias for path(). For example @RequestMapping("/foo") is equivalent to @RequestMapping(path="/foo"). – Hatice May 23 '18 at 06:44
0

There's no difference between @RequestMapping("/home") and @RequestMapping(value = "/home"). You can quickly use the former if there's only one mapping, and latter if there are multiple URLs that are being mapped to same place, for example, @RequestMapping(value={"/home","/home2","/home2/home3"})

Similarly, the path and value are also same. If you look at their definitions, they are basically alias of each other:

definition of value:

@AliasFor(value="path")
public abstract java.lang.String[] value
    ...

definition of path:

@AliasFor(value="value")
public abstract java.lang.String[] path
    ...

As for all the definitions, you can always visit the official docs.

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71