Ignoring the drama about the design in the question comments, the answer is that you should have a single method annotated with @PostMapping and then delegate to an appropriate handler function based on the facts.
In the original, you have two versions: /profile?userCpr=123 and /profile?userCprDetails=456. Both userCpr and userCprDetails are optional, so it's up to you to decide how to handle the situation where neither is provided or both are. But for the other use cases, simply declare both optional request params in the endpoint definition:
@PostMapping("/profile") public String showUser( @RequestParam(value = "userCpr", required = false) Long userCpr, @RequestParam(value = "userCprDetails", required = false) Long userCprDetails, Model model) { // if both are defined, or neither is defined, you need to decide how to handle that (or // if you should error) if (userCpr == null && userCprDetails == null) || (userCpr != null && userCprDetails != null) { // TODO: handle this situation } else if (userCpr != null) { return showUserAppointment(model); } else { return showUserDetails(model); } } public String showUserAppointment(Model model) { Long cpr = Long.parseLong(currentPrincipal()); model.addAttribute("iTestCenterService", iTestCenterService); model.addAttribute("userCpr", iAppointmentService.findAppointmentByCpr(cpr)); return "profile/profile"; } public String showUserDetails(Model model) { Long cpr = Long.parseLong(currentPrincipal()); model.addAttribute("iAddressService", iAddressService); model.addAttribute("userCprDetails", iUserService.findUserByCpr(cpr)); return "profile/profile"; }
We don't even bother passing the userCpr/userCprDetails values to the handler methods since we're going to ignore the value and use the result of currentPrincipal() instead.
Of course since the vast majority of both "handlers" is identical, you could just simplify to a single endpoint method and omit the "handler" methods entirely:
@PostMapping("/profile") public String showUser( @RequestParam(value = "userCpr", required = false) Long userCpr, @RequestParam(value = "userCprDetails", required = false) Long userCprDetails, Model model) { // if both are defined, or neither is defined, you need to decide how to handle that (or // if you should error) if (userCpr == null && userCprDetails == null) || (userCpr != null && userCprDetails != null) { // TODO: handle this situation } Long cpr = Long.parseLong(currentPrincipal()); model.addAttribute("iTestCenterService", iTestCenterService); if (userCpr != null) { model.addAttribute("userCpr", iAppointmentService.findAppointmentByCpr(cpr)); } else { model.addAttribute("userCprDetails", iUserService.findUserByCpr(cpr)); } return "profile/profile; }
... but if you're actually doing more stuff in those handler methods than it might make sense to keep that out of the endpoint itself and in distinct handler functions.
cprfrom request parameters and then immediately overwrite them with some opaque value.