Reputation: 11
I'm having a controller mapping as shown below
@RestController
@RequestMapping("/v1/connector")
public class Controller
and the API mapping as below
@GetMapping("2/auth")
when I hit the URL it's giving me the response as request URL not found. Can anyone tell why I'm getting this?
Upvotes: 1
Views: 2689
Reputation: 636
@GetMapping
is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. GET)
.
@RequestMapping
maps HTTP requests to handler methods of MVC and REST controllers.
When you use a base path on your controller class, all controllers in that class accept URL paths that start with your given base path. In your case:
@RestController
@RequestMapping("/v1/connector")
public class Controller {
...
}
This means all of the controllers inside this class have the base path of /v1/connector
and this means is a constant part of your URL and cant be changed.
So when you declare a @GetMapping("2/auth")
, Spring will automatically add a /
at the beginning of your path if there was no /
. And your path will be http://YOUR-HOST-NAME/v1/connector/2/auth
instead of http://YOUR-HOST-NAME/v1/connector2/auth
.
See here for more clarification.
So If your application has paths like /v1/connector1
, /v1/connector2
, /v1/connector3
, etc. It means the connector{n}
is not constant and you must declare it on each of your controllers' methods separately.
@RestController
@RequestMapping("/v1")
public class Controller {
@GetMapping("/connector2/auth")
...
@GetMapping("/connector3/auth")
...
.
.
.
}
Upvotes: 3