Search code examples
javaspringspring-restcontroller

Creating a replacement rest controller in spring with same name, causes bean name conflict?


Long story short, I want to replace the existing controller in spring boot because it is not satisfactory.

Therefore I have created a new rest controller class and have started adding functionality to it. I want to maintain the older controller until I can delete it in the future (once the newer version has been fully implemented)

So I have effectively two classes with the same name.

New Class

@RestController
@RequestMapping("/api/v2/parts")
public class PartController implements PartsApi {
...

Old Class

@RepositoryRestController
public class PartController {

When starting the service the following error occurs:

Annotation-specified bean name 'partController' for bean class [controller.v2.PartController] conflicts with existing, non-compatible bean definition of same name and class [controller.PartController]

I tried using the @Qualifier annotation but that does not compile.

How can I have two Rest classes with the same name in the spring boot app?

Note: I am loathe to try renaming PartController2


Solution

  • I had faced similar issue once and it got resolved after passing explicit name in @RestController annotation, which by default takes Class name at time of autowiring. Try this :

    V2 version:

    @RestController("PartControllerV2")
    @RequestMapping("/api/v2/parts")
    public class PartController implements PartsApi {
    

    V1 version:

    @RepositoryRestController("PartControllerV1")
    public class PartController {