Skip to content
Advertisement

springboot override controller methods

I have to ask because none of the answers I found can help me to solve my problem.

I have controller

@RestController
@RequestMapping("/path")
DefaultController

I want to override its methods because one specific customer has specific logics, with controller

@RestController
@RequestMapping("/path")
CustomController

that can have exactly same methods signatures or overriding methods with RequestBody extending the original requests.

  1. I tried to define an interface for controllers where I put the annotations
  2. I used @Primary and @Profile on CustomController to force Spring to load it rather than the default version

Solutions don’t work because Spring detects a double mapping for methods, the famous “Ambiguous mapping”.

Has anybody any idea how i could solve the situation?

I want literally plug custom implementations of controllers, only where I need, overriding only methods I need, without altering request path. Customizing Services is not enough, because sometimes I have to pass RequestBody with additional fields

We are using springboot 2.4.7, spring 5.3.8

Thanks in advance

Advertisement

Answer

I was finally able to solve my problem. I did this, if anybody interested

  1. assigned a specific profile to overriding controller via @Profile(“what you want”)
  2. added custom header to controller request mapping, via @RequestMapping attribute “headers”
  3. only for post and put methods, I had to set a value again for attribute “headers”, both on original and custom controller
Advertisement