The key is that the @RestController
class is from a 3rd party package that I can't touch. I want all the other classes in that package but not a particular @RestController
because I want to replace those endpoints with my own.
It also seems this class is @Import'ed by another class that's also in the 3rd party package that I don't control (see Edit3 below)
It's similar to this question/answer, however in that question, OP has access to the @RestController and can append a @ConditionalOnExpression, whereas I cannot.
Is there another way to disable a @RestController in spring?
The specific @RestController class I want to disable is GraphQLController.
Edit1
Here are some ways I tried to exclude it but fails
@ConfigurationPropertiesScan("com.mycomp.package")
@SpringBootApplication(exclude = {graphql.kickstart.spring.webflux.GraphQLController.class})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
The above attempt throws a
java.lang.IllegalStateException: The following classes could not be excluded because they are not auto-configuration classes: - graphql.kickstart.spring.webflux.GraphQLController
As suggested below, I also tried @ComponentScan.Filter
of ASSIGNABLE_TYPE
@ConfigurationPropertiesScan("com.mycomp.package")
@ComponentScan(excludeFilters={
@ComponentScan.Filter(type= FilterType.ASSIGNABLE_TYPE, value=graphql.kickstart.spring.webflux.GraphQLController.class)})
@SpringBootApplication
public class Application {
....
}
No errors but it's still instantiating the bean.
Also tried @ComponentScan.Filter
of REGEX
@ConfigurationPropertiesScan("com.mycomp.package")
@ComponentScan(excludeFilters={
@ComponentScan.Filter(type= FilterType.REGEX, pattern = "graphql.kickstart.spring.webflux.GraphQLController")})
@SpringBootApplication
public class Application {
....
}
That didn't seem to work either
Also tried @ComponentScan.Filter
of CUSTOM
@ConfigurationPropertiesScan("com.mycomp.package")
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.CUSTOM,
classes = GraphQLControllerBeanFilter.class))
@SpringBootApplication
public class Application {
....
}
public class GraphQLControllerBeanFilter implements TypeFilter {
@Override
public boolean match(MetadataReader metadataReader,
MetadataReaderFactory metadataReaderFactory) {
ClassMetadata classMetadata = metadataReader.getClassMetadata();
String fullyQualifiedName = classMetadata.getClassName();
return fullyQualifiedName.equals("graphql.kickstart.spring.webflux.GraphQLController");
}
}
The custom filter doesn't seem to ever see the GraphQLController
. In fact, I printed out all the fullyQualifiedName
it scans and only the ones in my com.mycomp.package
package show. Nothing from 3rd party libraries even though it's loading them.
I'm wondering if the @ComponentScan.Filter
ever sees 3rd party packages or if maybe I have some other spring setting overriding it.
I also tried adding @Primary
to my Controller
@Primary
@RestController
public class MyGraphQLController extends graphql.kickstart.spring.webflux.GraphQLController
But that results in this error
java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'graphql.kickstart.spring.webflux.GraphQLController'
Edit2
This is the 3rd party library that contains the @RestController I'm trying to exclude
implementation "com.graphql-java-kickstart:graphql-kickstart-spring-boot-starter-webflux:11.0.0"
Edit3
It looks like graphql.kickstart.spring.webflux.boot.GraphQLSpringWebfluxAutoConfiguration is importing the GraphQLController
in an @Import
annotation.
Is there anyway to disable or replace it in this case?