Getting REST version from request mapping of controller for mockmvc

When we write a spring rest controller we give version url and start writing different functions as endpoints like below @RestController @RequestMapping("/api/v1") public class ControllerApi { final LoginSender loginSender; public ControllerApi(LoginSender loginSender) { this.loginSender = loginSender; } @PostMapping("/login") boolean login(@RequestBody LoginParams params) { return loginSender.login(params.server(), params.login(), params.password(), params.build(), params.webManager()); } @GetMapping("/get1") LoginParams get1(@RequestParam String login, @RequestParam(required = false) String webMan, @RequestParam(required = false) String server, @RequestParam(required = false) String pass, @RequestParam(required = false) String build) { return LoginParams.builder() .webManager(webMan) .server(server) .password(pass) .login(login) .build(build) .build(); } } ib the future this controller will have different version number from v1 to v2 or in the future someone will change login endpoint from “/login” to “/loginJwt” but function order probably will not change. So in order to prevent your tests to fail we can collect these data from controller class like the code below. ...

December 12, 2022 · 2 min · Özkan Pakdil